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
  • 36,774
  • Why not do this with cron? It would certainly be much easier if you know the exact interval a command must be run at. – John Cave Apr 15 '15 at 09:24
  • Do you want this to run in a Terminal or as a background process? Do you want this to run automatically at startup or you're fine with just running the command I posted in my answer? Lastly, do you need to check the output of the inner commands? (i.e. either in a Terminal and / or in a log file if you want this to run in a Terminal or in a log file if you want this to run as a background process) – kos Apr 15 '15 at 10:25
  • ^ kos, I want to run it as a background process upon system startup. I assume that all that I have to do is to add this: sudo service start codesys && watch -n 7200 'sudo service stop codesys && sudo service start codesys' (The code you wrote earlier) to a file inside the init.d folder? – Pinn the Angled one Apr 15 '15 at 10:38
  • Please read about X-Y problem and refrain from asking one.. – heemayl Apr 15 '15 at 10:41
  • @JoachimIdland I added the instructions on how to start the command at startup if you want to check them out – kos Apr 17 '15 at 08:31
  • look at Lekensteyn's answer to the question muru pointed out. – guntbert Apr 17 '15 at 20:33

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
  • 35,891
  • 1
    +1..although this kind of thing is good to run in the background with nohup or disown so that the closure of terminal session has no effect on the process, although if OP wants to check the STDOUT & STDERR, should use nohup as it saves the output & error in a file $PWD/nohup.out (unless other file is mentioned).. – heemayl Apr 15 '15 at 10:08
  • @heemayl You're definetly right, I was updating this to include how to run it at startup but it's not clear whether OP actually needs to check the output, I'll update including nohup or disown if necessary once OP will have explained this – kos Apr 15 '15 at 10:16
  • Sorry for benig unclear, what I actually want it to do is to start the process and shut it down every 2 hours, I do not need the output checked. The startup script that you sent me, does it run the cycle at startup or does it start the cycle 2 hours AFTER the initial bootup? – Pinn the Angled one Apr 15 '15 at 10:25
  • 1
    @JoachimIdland I updated the command to start codesys at execution time, what the command does is this: 1. Starts codesys; 2. Every 7200 seconds stops and restarts codesys, until the script is killed, so as of now if run in a Terminal, it will do the above displaying the output in the terminal itself, and such Terminal should not be closed, or the script would be killed. Let me know if you need something different by answering the questions in the comment i left to your question, to clarify all possible cases – kos Apr 15 '15 at 10:32
  • 1
    you shall add the above script to /etc/rc.local to let it a startup script without change the init.d. Just add in /etc/rc.local before the string exit 0 the command: service start codesys && watch -n 7200 'service stop codesys && service start codesys' – feligiotti Apr 15 '15 at 11:01
  • IMO it depends on the version of Ubuntu. I would either write a proper upstart or systemd "init" – Panther Apr 15 '15 at 14:28