0

I want to make a simple init.d file that makes it possible for me to reset a trial service that I'm using. I am currently using CodeSys and the startup script cuts me off after 2 hours, so I need to either reset the machine or manually restart the service.

Could someone help me to make a script that:

service stop codesys
service start codesys

(every 7200 seconds in an infinite loop)

Thanks in advance!

EDIT: Here is the startup script that has been initially included, do you find anything related to a 2 hour timer? I really don't know much about this stuff.

http://pastebin.com/vzWi3FBN

thomasrutter
  • 37,804

1 Answers1

4

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.

kos
  • 41,268