2

G'day, trying to get an expect script that runs a telnet session to run on shutdown of a VM running in vmware player in a VNC session. It runs just fine when I run systemctl stop by hand, but when I do a shutdown -r now, it doesn't happen.

My service file :

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
Before=shutdown.target reboot.target halt.target

[Service]
Type=forking

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/root/vmware/SCO/stopsco.pl
ExecStop=/usr/bin/sleep 10
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

It's my understanding that the three ExecStop lines should get run, in order, and they do when I run systemctl stop vncserver@:1.service But when I shutdown the server, they don't get run - the sleep 10 would stop the VNC server going down for 10 seconds, which I should be able to see, but it doesn't, it just goes crunch and the VNC server stops. So the last line is being run, for sure, but the prior two do not seem to be, or if they are, they're not being waited for - the "stopsco" script takes about 60 seconds to run. Can anyone see what I'm missing? I'm new to systemd, this stuff worked fine with rc scripts :)

I've fussed around with the suggestion here : systemd to wait for command to complete before restart/shutdown or killing other processes and added

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
Before=shutdown.target reboot.target halt.target

but it still just slammed the VNC session shut, it certainly didn't wait for 10s when I ran shutdown -r, but again, it works perfectly when run by hand.

Update :

systemctl status vncserver@:1.service
▒ vncserver@:1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-09-12 00:42:04 AEST; 9h ago
  Process: 3867 ExecStop=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
  Process: 3864 ExecStop=/usr/bin/sleep 10 (code=exited, status=0/SUCCESS)
  Process: 3831 ExecStop=/root/vmware/SCO/stopsco.pl (code=exited, status=0/SUCCESS)
  Process: 3968 ExecStart=/usr/sbin/runuser -l root -c /usr/bin/vncserver %i (code=exited, status=0/SUCCESS)
  Process: 3963 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 4002 (Xvnc)
   CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service
           ▒ 4002 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop tardis.li...

                                                                               Sep 12 00:42:01 tardis.lilydaleairport.com.au systemd[1]: Starting Remote des...
Sep 12 00:42:04 tardis.lilydaleairport.com.au systemd[1]: Started Remote desk...
Hint: Some lines were ellipsized, use -l to show in full.
[root@tardis system]#

systemd is version

systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
  • 1
    btw, CentOS 7, if it's not clear from the tag. – Carl Ivan Brewer Sep 11 '18 at 14:19
  • Please mention your systemd version (from `systemd --version`). Also confirm with `systemctl status yourservice.service` that the service is actually running up and running before it automatically shut down. If it not running, `ExecStop=` directives won't work at shutdown time. – Mark Stosberg Sep 11 '18 at 15:35
  • systemctl --version systemd 219 +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN – Carl Ivan Brewer Sep 12 '18 at 00:26
  • Does stopsco.pl fork? – Mark Stosberg Sep 12 '18 at 11:57
  • No, it doesn't, it runs an expect script that telnets to the guest and shuts it down (I know ... ). It takes about 30 seconds or so to run and exits. – Carl Ivan Brewer Sep 13 '18 at 02:10
  • Question title says `shutdown`, but question bodytext says `shutdown -r`, with `... -r` indicating *reboot*. ... I had similar odd behavior with *lxc* containers: A host **shutdown** would quickly finish, whereas a host **reboot** would cause an additional 60 seconds during the "go-down" phase.... it turns out that when the **host** systemd manager initiates a **reboot**, it sends a reboot kill signal to the lxc container systemd managers. Therefore the lxc containers reboot instead of shutting down. From the host systemd manager's perspective, the process looks stuck, sends a kill signal. – Abdull Jul 17 '23 at 07:08

0 Answers0