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

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:


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


removes the job from cron

How can I achieve this?

Thank you

  • 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

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.


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...

  • 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
  touch "/tmp/$0.lock"
# 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.

  • Happy to have helped! – DopeGhoti Aug 28 '13 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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