Applications and scripts can be stopped and started automatically in the guest shell environment using systemd.
The below script named /home/guestshell/datecap.sh will save a file in the /tmp named datecap:
#!/bin/bash
OUTPUTFILE=/tmp/datecap
rm -f $OUTPUTFILE
while true
do
echo $(date) >> $OUTPUTFILE
echo 'Hello World' >> $OUTPUTFILE
sleep 10
done
This script can be tied into the systemd infrastructure with the following file named /usr/lib/systemd/system/datecap.service:
[Unit]
Description=Trivial "datecap" example daemon
[Service]
ExecStart=/home/guestshell/datecap.sh &
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
After creating the two files, the user can use systemctl to start and stop the datecap process:
[guestshell@guestshell tmp]sudo systemctl start datecap
[guestshell@guestshell tmp]$ sudo systemctl status -l datecap
datecap.service - Trivial "datecap" example daemon
Loaded: loaded (/usr/lib/systemd/system/datecap.service; disabled)
Active: active (running) since Wed 2015-09-30 02:52:00 UTC; 2min 28s ago
Main PID: 131 (datecap.sh)
CGroup: /system.slice/datecap.service
??131 /bin/bash /home/guestshell/datecap.sh &
??164 sleep 10
Sep 30 02:52:00 guestshell systemd[1]: Started Trivial "datecap" example daemon.
[guestshell@guestshell tmp]$ sudo systemctl stop datecap
When the device is rebooted, systemd will automatically start the datecap daemon.