Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a ubuntu server where I put my application core. It's a jar that needs to be always listening for requests. I made a script to run it with the correct parameters.

The scenario is similar to this: script location: /home/andrea/core/script.sh

script.sh takes a parameter, "start" or "stop" depending what I need to do.

Since I need it to be alive I created a cron job that checks if it's alive and if not it starts it again with the following commands:

crontab -e
*/5 * * * * /home/andrea/core/testscriptalive.sh

where testscriptalive.sh is: #!/bin/bash

# Check if core is running
if [ ! -f /tmp/core.pid ]; then 
  cd /home/andrea/core/
  ./script.sh start
  ./email.sh
fi

So, every 5 minutes cron runs the script "testscriptalive.sh", which basically checks if there's the correct PID and if not it starts again the core and it sends me an email. So far so well. What I need to do is: if I want to stop the core for X reasons how can I stop cron job from executing without editing it manually? For example if in the console I enter "./script stop" after 5 minutes the cron job automatically restarts it even if I don't want to. I need two sh scripts to couple to my "start" and "stop" parameters that:

start:

checks if the job is correctly stored in cron, if not adds it

stop:

removes the job from cron

How can I achieve this?

Thank you

share|improve this question
    
I'd also check to see if there is a process that matches your PID (didn't crash and leave tmpfile). read PROCPID < /tmp/core.pid; [ -w /proc/$PROCPID/mem ]; would be a good test on Linux. –  Rich Homolka Aug 13 '13 at 17:36
    
Thank you, I'm going to integrate your check into my scripts –  Andrea Aug 22 '13 at 10:45

3 Answers 3

up vote 0 down vote accepted

One idea is to create a couple of text files that contain your cron jobs. One with the normal list of cron jobs, then another one with the normal list plus this job. Then, you can load the crontab from the text files.

crontab /path/to/MyCronJobs.txt

That will overwrite crontab with the jobs in the text file.

share|improve this answer
    
Thank you, that's what I ended doing –  Andrea Aug 22 '13 at 10:44
    
You're welcome. –  Jack Aug 22 '13 at 14:34

You need to add check into stop procedure of script.sh: If script.sh run from shell(not cron) --> create some /tmp/stop.tmp file, and delete this file in start procedure.

So testscriptalive.sh also need to check if /tmp/stop.tmp exists...

share|improve this answer
    
How can I differentiate if script.sh is run from shell and not from cron? –  Andrea Aug 22 '13 at 10:45
    
@Andrea : Use tty. More: man tty –  september Aug 22 '13 at 16:59

The best solution I have found to needs like this is to add locking to all of my such scripts with e. g.:

if [[ -f "/tmp/$0.lock" ]]; then
  echo "$0 is locked.  Bailing."
  exit 0
else
  touch "/tmp/$0.lock"
fi
# the rest of the script goes here
rm -f "/tmp/$0.lock"

Once this is in my scripts, I can manually touch /tmp/scriptname.lock to place the lock file when I want to keep the cron job from running - cron will still fire off the script, but the script will see its lock file and abort.

share|improve this answer
    
Thank you, brilliant idea –  Andrea Aug 22 '13 at 10:45
    
Happy to have helped! –  DopeGhoti Aug 28 '13 at 22:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.