Bagaimana cara mengetahui apakah suatu program sedang berjalan atau menginginkan input pengguna
Itu tergantung pada program dan bagaimana Anda memintanya.
Seringkali tetapi tidak selalu akan ada prompt, yang menunjukkan bahwa program meminta masukan.
Jika Anda tidak yakin, Anda dapat memeriksa apakah proses program sedang sibuk
Dan ketika program telah selesai, Anda akan melihat prompt shell.
Shellscript running
Saya memiliki shellscript yang memeriksa apakah suatu program sedang berjalan, dan sekarang saya telah menambahkan opsi -s
untuk membuatnya berjalan sudo strace -f -p <PID>
(sesuai dengan jawaban Sergiy Kolodyazhnyy) ketika sebuah ... ditemukan.
Shellscript menggunakan
Pemakaian
$ ./running
Usage: ./running <program-name>
./running <part of program name>
Examples: ./running firefox
./running term # part of program name
./running dbus
./running 'dbus-daemon --session' # words with quotes
./running -v term # verbose output
./running -s term # strace checks activity
Anda dapat menginstal shellscript running
ke direktori PATH
jika Anda ingin akses mudah ke sana.
Kode shellscript
#!/bin/bash
# date sign comment
# 2019-02-14 sudodus version 1.0
verbose=false
strace=false
if [ "$1" == "-v" ]
then
verbose=true
shift
fi
if [ "$1" == "-s" ]
then
strace=true
shift
fi
if [ $# -ne 1 ]
then
echo "Usage: $0 <program-name>
$0 <part of program name>
Examples: $0 firefox
$0 term # part of program name
$0 dbus
$0 'dbus-daemon --session' # words with quotes
$0 -v term # verbose output
$0 -s term # strace checks activity"
exit
fi
inversvid="\0033[7m"
resetvid="\0033[0m"
redback="\0033[1;37;41m"
greenback="\0033[1;37;42m"
blueback="\0033[1;37;44m"
runn=false
#tmpfil=$(mktemp)
tmpdir=$(mktemp -d)
tmpfil="$tmpdir/tmpfil"
vtfile="$tmpdir/vtfile"
vthead="$tmpdir/vthead"
# check by systemctl
systemctl is-active --quiet "$1"
if [ $? -eq 0 ]
then
echo "systemctl is-active:"
runn=true
fi
# check by ps
ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8- | grep "$1" | grep -vE -e "$0 *$1" -e "$0 *.* *$1" -e "grep $1" | sort -u > "$tmpfil"
#cat "$tmpfil"
if $verbose || $strace
then
ps -ef |head -n1 > "$vthead"
ps -ef | grep "$1" | grep -vE -e "$0 *.* *$1" -e "grep $1" | sort -u > "$vtfile"
fi
tmpstr=$(head -n1 "$tmpfil")
#echo "tmpstr=$tmpstr"
tmpess=$(grep -om1 "$1" "$tmpfil")
#echo "tmpess=$tmpess"
if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ] || [ "$tmpess" == "$1" ]
then
echo "ps -ef: active:"
runn=true
if $verbose
then
cat "$vthead" "$vtfile"
fi
elif test -s "$tmpfil"
then
if $runn
then
echo "----- consider also ------------------------------------------------------------"
if $verbose
then
cat "$vthead" "$vtfile"
else
cat "$tmpfil"
fi
echo "--------------------------------------------------------------------------------"
else
echo "----- try with: ----------------------------------------------------------------"
if $verbose
then
cat "$vthead" "$vtfile"
else
cat "$tmpfil"
fi
echo "--------------------------------------------------------------------------------"
fi
fi
if $runn
then
echo -en "$greenback '$1"
if [ "$tmpstr" != "$tmpess" ]
then
echo -n " ..."
fi
echo -e "' is running $resetvid"
if $strace
then
which xterm
if [ $? -eq 0 ]
then
pid=$(head -n1 "$vtfile" | sed 's/^ *//' | tr -s ' ' '\t' | cut -f 2)
echo "checking pid=$pid; quit with 'ctrl + c' in the xterm window"
xterm -title "'strace' checks '$1'" 2> /dev/null -e sudo strace -f -p $pid
else
echo "Please install 'xterm' for this function to work"
exit
fi
fi
else
inpath=$(which "$1")
if [ "$inpath" == "" ]
then
echo -e "$redback no path found to '$1' $resetvid"
else
echo -e "$blueback '$1' is not running $resetvid"
fi
fi
rm -r "$tmpdir"
Demo
Memeriksa terminal windows di Lubuntu (LXTerminal dimulai sebagai x-terminal-emulator
dan custom gnome-terminal
windows),
$ running -v -s term
----- try with: ----------------------------------------------------------------
UID PID PPID C STIME TTY TIME CMD
sudodus 2087 1384 0 13:33 ? 00:00:00 x-terminal-emulator
sudodus 2108 1269 0 13:33 ? 00:00:17 /usr/lib/gnome-terminal/gnome-terminal-server
--------------------------------------------------------------------------------
no path found to 'term'
$ running -v -s x-terminal-emulator
ps -ef: active:
UID PID PPID C STIME TTY TIME CMD
sudodus 2087 1384 0 13:33 ? 00:00:00 x-terminal-emulator
'x-terminal-emulator' is running
/usr/bin/xterm
checking pid=2087; quit with 'ctrl + c' in the xterm window
Ada banyak aktivitas segera setelah kursor berada di jendela terminal.
Mulai grep
(menunggu input dari /dev/stdin
)
$ grep -i --color 'hello'
asdf
Hello World
Hello World
Memeriksanya
$ running -s grep
ps -ef: active:
'grep ...' is running
/usr/bin/xterm
checking pid=14982; quit with 'ctrl + c' in the xterm window
Tidak banyak kegiatan, dan Anda dapat mengidentifikasi apa yang terjadi.
PS1
prompt.