Bagi saya untuk membaca status layanan untuk service
aplikasi:
$ /sbin/service network status
network.service - Network Connectivity
Loaded: loaded (/lib/systemd/system/network.service; enabled)
Active: active (exited) since Ср 2014-01-29 22:00:06 MSK; 1 day 15h ago
Process: 15491 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
$ /sbin/service httpd status
httpd.service - SYSV: Apache is a World Wide Web server. It is used to serve HTML files and CGI.
Loaded: loaded (/etc/rc.d/init.d/httpd)
Active: activating (start) since Пт 2014-01-31 13:59:06 MSK; 930ms ago
dan itu bisa dilakukan dengan kode:
function is_in_activation {
activation=$(/sbin/service "$1" status | grep "Active: activation" )
if [ -z "$activation" ]; then
true;
else
false;
fi
return $?;
}
while is_in_activation network ; do true; done
Gunakan grep -q
. -q
opsi membuat grep
tenang, dan akan segera keluar saat teks muncul.
Perintah di bawah ini memulai ./some-service
di latar belakang, dan memblokir hingga "Server aktif" muncul di stdout.
(./some-service &) | grep -q "Server is active"
Saya akan melakukannya dengan cara ini.
./server > /tmp/server-log.txt &
sleep 1
while ! grep -m1 'Server is active' < /tmp/server-log.txt; do
sleep 1
done
echo Continue
Di sini -m1
memberi tahu grep(1)
untuk berhenti di pertandingan pertama.
Saya membenarkan jawaban saya dengan "layanan" mainan saya di bawah:
#! /bin/bash
trap "echo 'YOU killed me with SIGPIPE!' 1>&2 " SIGPIPE
rm -f /tmp/server-output.txt
for (( i=0; i<5; ++i )); do
echo "i==$i"
sleep 1;
done
echo "Server is active"
for (( ; i<10; ++i )); do
echo "i==$i"
sleep 1;
done
echo "Server is shutting down..." > /tmp/server-output.txt
Jika Anda mengganti echo Continue
dengan echo Continue; sleep 1; ls /tmp/server-msg.txt
, Anda akan melihat ls: cannot access /tmp/server-output.txt: No such file or directory
yang membuktikan tindakan "Lanjutkan" dipicu tepat setelah keluaran Server is active
.