I'm trying to run a bash script at boot time from /etc/rc.local on a headless Raspberry Pi 4 (Raspbian buster lite - Debian based). I've done something similar on a Pi 3 with success so I'm confused about why the Pi 4 would misbehave - or behave differently. The script executed from /etc/rc.local fires but appears to just exit at seemingly random intervals with no indication as to why it's being terminated.
To test it, I dumbed down the script and just stuck the following into a test script called /home/pi/test.sh:
#!/bin/bash
while true
do
echo 'Still alive'
sleep 1
done
I then call it from /etc/rc.local just before the exit line:
#!/bin/sh -e
#
# rc.local - executed at the end of each multiuser runlevel
#
# Make sure that the script will "exit 0" on success or any other
# value on error.
openvt -c 1 -f -- /home/pi/test.sh
echo $? >/tmp/exiterr #output exit code to /tmp/exiterr
exit 0
The contents of /tmp/output:
Still alive
Still alive
and /tmp/exiterr shows
0
If I reduce the sleep period, /tmp/output is longer (over 6000 lines without the sleep). Any ideas why the script is exiting shortly after starting?
EDIT I notice the script runs fine from rc.local as well as crontab @reboot if I omit openvt. I have also tried creating a system.d service that calls the full command including openvt.
The .service file looks like this:
[Unit]
Description=Rpi Player
After=network.target
[Service]
ExecStart=/home/pi/testcaller.sh
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
[Install]
WantedBy=multi-user.target
...and the testcaller.sh looks like this:
#!/bin/bash
/bin/openvt -c 1 -f -- /home/pi/test.sh
EDIT added for Inigo: After reboot, the output of sudo systemctl status player.service is:
● player.service - Rpi Player
Loaded: loaded (/etc/systemd/system/player.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2019-12-29 10:02:54 GMT; 34s ago
Process: 544 ExecStart=/bin/bash -c /bin/openvt -c 1 -f -- /home/pi/test.sh (code=exited, status=0/SUCCESS)
Main PID: 544 (code=exited, status=0/SUCCESS)
Dec 29 10:02:54 raspberrypi systemd[1]: Started Rpi Player.
Dec 29 10:02:54 raspberrypi systemd[1]: player.service: Succeeded.
The output of sudo ps -aux | grep /home/pi shows that the service is no longer running (as expected per code=exited above). If I start the service manually from the shell using sudo systemctl start player.service, it does the same - it starts then exits. If I run the execstart command from the bash prompt, it runs and behaves as expected:
sudo /bin/bash -c '/bin/openvt -c 1 -f -- /home/pi/test.sh'
The output of journalctl --unit=player.service:
-- Logs begin at Sun 2019-12-29 10:13:18 GMT, end at Sun 2019-12-29 10:16:17 GMT. --
Dec 29 10:13:33 raspberrypi systemd[1]: Started Rpi Player.
Dec 29 10:13:33 raspberrypi systemd[1]: player.service: Succeeded.
Why can I not run this simple openvt command on a Raspberry Pi 4 as a service or cron job?