The most straightforward way is to just run once:
sudo service start codesys && watch -n 7200 'sudo service stop codesys && sudo service start codesys'
This will execute the command sudo service stop codesys && sudo service start codesys
(which stops the codesys
service and on success subsequently starts it again) every 7200
seconds displaying potential output.
Notice that in this case since the command to be executed is actually a chain of two commands the quotes to enclose the command are required.
Here's a scrape of the relevant sections for watch
from man watch (trusty)
:
[...]
DESCRIPTION
watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output change
over time. By default, the program is run every 2 seconds. By
default, watch will run until interrupted.
OPTIONS
[...]
-n, --interval seconds
Specify update interval. The command will not allow quicker
than 0.1 second interval, in which the smaller values are
converted.
[...]
To make it run at startup, add this line to /etc/rc.local
before the line exit 0
:
nohup sh -c "sudo service start codesys && watch -n 7200 \"sudo service stop codesys && sudo service start codesys\"" > /dev/null &
nohup
starts a process immune to SIGHUP
signals, redirects the process' stdin
to /dev/null
and both stdout
and stderr
to a nohup.out
file, in this case stdout
and stderr
are redirected to /dev/null
since you don't need to check the output. To use nohup
is done so that the process is not killed when the execution of /etc/rc.local
terminates. The &
operator puts the process in the background, so that the execution of /etc/rc.local
can continue.