मशीन शटडाउन / रिबूट से पहले इनायत से बाहर निकलने के लिए सेवा की प्रतीक्षा करें


9

आप एक प्रणालीगत सेवा कैसे लिखते हैं जो मशीन शटडाउन या रिबूट पर शान से बंद हो जाती है? विशेष रूप से, यह मशीन शटडाउन में देरी करना चाहिए जब तक कि यह इनायत से बाहर नहीं निकलता है।

मेरे पास एक सेवा है जिसे बंद करने में 10 सेकंड लगते हैं: /usr/local/bin/shutdowntest.sh:

#!/bin/bash

SHUTDOWN=0
SHUTDOWN_TIME=10
TRAPPED_SIGNAL=

function onexit() {
  TRAPPED_SIGNAL=$1
  SHUTDOWN=1
}

for SIGNAL in SIGINT SIGTERM SIGHUP SIGPIPE SIGALRM SIGUSR1 SIGUSR2; do
  trap "onexit $SIGNAL" $SIGNAL
done

echo >&2 "shutdowntest running"
while ((!SHUTDOWN || SHUTDOWN_TIME>0)); do
  if [[ -n "$TRAPPED_SIGNAL" ]]; then
    echo >&2 "shutdowntest received signal $TRAPPED_SIGNAL"
    TRAPPED_SIGNAL=
  elif ((SHUTDOWN)); then
    echo >&2 "shutdowntest Shutting down: $SHUTDOWN_TIME more sleeps"
    SHUTDOWN_TIME=$((SHUTDOWN_TIME-1))
    sleep 1
  else
    sleep 10
  fi
done
echo >&2 "shutdowntest Finished shutting down; quitting"

मैंने TimeetStopSec को 15s में /etc/systemd/system/shutdowntest.service पर सेट किया है:

[Service]
ExecStart=/usr/local/bin/shutdowntest.sh
TimeoutStopSec=15

[Install]
WantedBy=multi-user.target

जब मैं दौड़ता हूं sudo systemctl stop shutdowntest.service, तो सेवा उसके अनुसार शान से बंद हो जाती है /var/log/syslog:

00:57:11 shutdowntest.sh[1980]: shutdowntest received signal SIGTERM
00:57:11 shutdowntest.sh[1980]: shutdowntest Shutting down: 10 more sleeps
00:57:11 systemd[1]: Stopping shutdowntest.service...
00:57:11 shutdowntest.sh[1980]: Terminated
00:57:11 shutdowntest.sh[1980]: shutdowntest Shutting down: 9 more sleeps
00:57:12 shutdowntest.sh[1980]: shutdowntest Shutting down: 8 more sleeps
00:57:13 shutdowntest.sh[1980]: shutdowntest Shutting down: 7 more sleeps
00:57:14 shutdowntest.sh[1980]: shutdowntest Shutting down: 6 more sleeps
00:57:15 shutdowntest.sh[1980]: shutdowntest Shutting down: 5 more sleeps
00:57:16 shutdowntest.sh[1980]: shutdowntest Shutting down: 4 more sleeps
00:57:17 shutdowntest.sh[1980]: shutdowntest Shutting down: 3 more sleeps
00:57:18 shutdowntest.sh[1980]: shutdowntest Shutting down: 2 more sleeps
00:57:19 shutdowntest.sh[1980]: shutdowntest Shutting down: 1 more sleeps
00:57:20 shutdowntest.sh[1980]: shutdowntest Finished shutting down; quitting
00:57:20 systemd[1]: Stopped shutdowntest.service.

लेकिन जब मैं sudo rebootया sudo shutdown nowमशीन, सेवा को बिना इज़्ज़त के बाहर निकलने के लिए पर्याप्त समय के बिना मार दिया जाता है, और / var / log / syslog केवल 1s बाद में समाप्त होता है।

00:59:30 shutdowntest.sh[2014]: Terminated
00:59:30 shutdowntest.sh[2014]: shutdowntest received signal SIGTERM
00:59:30 shutdowntest.sh[2014]: shutdowntest Shutting down: 10 more sleeps
00:59:30 systemd[1]: Stopping shutdowntest.service...

यह सुनिश्चित करने के लिए कि मशीन बंद होने या रिबूट होने पर सेवा को बाहर निकलने का समय ( TimeoutSecया TimeoutStopSec) दिया जाता है ?


1
systemd को पहले ही सेवा बंद होने का इंतजार करना चाहिए। क्या आप journalctl -u shutdowntest.serviceसीधे देखने के बजाय पत्रिका ( ) में देख सकते हैं /var/log/syslog? मुझे आश्चर्य है कि अगर आपकी सेवा से पहले syslog बंद नहीं हुआ है और फिर बाकी आउटपुट लॉग नहीं कर रहा है
Bigon

जवाबों:


4

बिगोन की टिप्पणी सही है। मैं देख रहा था /var/log/syslog, लेकिन इसके द्वारा लिखा गया है rsyslog.service, जो शटडाउन प्रक्रिया में बहुत जल्दी बंद हो जाता है (जैसा कि नीचे "बंद सिस्टम लॉगिंग सेवा" द्वारा इंगित किया गया है)।

बाद मैं लगातार journald प्रवेश बजाय सक्षम ( Storage=persistentमें /etc/systemd/journald.confऔर systemctl restart systemd-journald), journalctl -b-1 -u shutdowntest.serviceसे पता चलता है कि मेरी सेवा वास्तव में प्रणाली के बाद बंद करने के लिए पर्याप्त समय दिया जाता है rebootएड, shutdownया बाद में बिजली की कुंजी दबाने (ACPI G2 शीतल बंद)।

journalctl -b-1 -u shutdowntest.service -u rsyslog.service -u systemd-logind
-- Logs begin at Mon 2018-03-26 18:39:12 UTC, end at Mon 2018-03-26 20:22:34 UTC. --
…
Mar 26 18:46:46 myhost systemd-logind[1202]: Power key pressed.
Mar 26 18:46:46 myhost systemd-logind[1202]: Powering Off...
Mar 26 18:46:46 myhost systemd-logind[1202]: System is powering down.
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: Terminated
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: shutdowntest received signal SIGTERM
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 10 more sleeps
Mar 26 18:46:46 myhost systemd[1]: Stopping shutdowntest.service...
Mar 26 18:46:46 myhost systemd[1]: Stopping Login Service...
Mar 26 18:46:46 myhost systemd[1]: Stopped Login Service.
Mar 26 18:46:46 myhost systemd[1]: Stopping System Logging Service...
Mar 26 18:46:47 myhost systemd[1]: Stopped System Logging Service.
Mar 26 18:46:47 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 9 more sleeps
Mar 26 18:46:48 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 8 more sleeps
Mar 26 18:46:49 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 7 more sleeps
Mar 26 18:46:50 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 6 more sleeps
Mar 26 18:46:51 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 5 more sleeps
Mar 26 18:46:52 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 4 more sleeps
Mar 26 18:46:53 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 3 more sleeps
Mar 26 18:46:54 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 2 more sleeps
Mar 26 18:46:55 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 1 more sleeps
Mar 26 18:46:56 myhost shutdowntest.sh[1237]: shutdowntest Finished shutting down; quitting
Mar 26 18:46:56 myhost systemd[1]: Stopped shutdowntest.service.
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.