簡體   English   中英

在腳本中,如何使用 ttyecho 獲取生成的終端 shell 的 pid 以在其中執行命令?

[英]In a script how to get the pid of spawned terminal's shell to execute commands in it using ttyecho?

我正在使用 ttyecho(可以與yay -S ttyecho-git一起安裝)在單獨的終端中執行命令,如下所示:

urxvt &
sudo ttyecho -n /proc/<pid-of-new-urxvt>/fd/0 <command>

它不起作用,因為 /proc/pid-of-new-urxvt/fd/0 是指向父終端的 /dev/pts/x 的符號鏈接。 在生成的 urxvt 中,我碰巧運行了 zsh。 因此,如果我使用該 zsh 進程的 pid,它會起作用:

sudo ttyecho -n /proc/<pid-of-new-zsh-within-new-urxvt>/fd/0 <command>

當我運行urxvt &時,如何獲取在新 urxvt 進程中生成的新 zsh 進程的 pid? 或者是否有不同的解決方案來實現相同的結果?

pgrep -P <pid-of-new-urxvt>給出子 zsh 進程的 pid。 感謝@user1934428 進行頭腦風暴

這是生成的 bash 腳本:

urxvt &
term_pid=$!

# sleep here makes us wait until the child shell in the terminal is started
sleep 0.1

# we retrieve the pid of the shell launched in the new terminal
shell_pid=$(pgrep -P $term_pid)

# ttyecho executes the command in the shell of the new terminal and gives back control of the terminal so you can run further commands manually
sudo ttyecho -n /proc/${shell_pid}/fd/0 "$@"

因此,當我啟動“script ls”時,它會打開一個新終端,運行 ls,並在終端仍然打開的情況下返回提示。 我只需要在 sudoers 文件中添加 ttyecho 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM