Cron job to prevent simultaneous script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job to prevent simultaneous script
# 1  
Old 06-16-2010
Cron job to prevent simultaneous script

I'm using a shared server on Hostgator (Linux CentOS). I'm trying to set a cron job using the Control Panel that will check if its already running before starting a new one.

I've tried the following...

Code:
* * * * * [ -z "$(ps ax|grep /pathtoscript/parse_script.php|grep -v grep)" ] && [ php /pathtoscript/parse_script.php ]


but I get this error emailed to me...

Code:
/bin/sh: line 0: [: php: unary operator expected


Any ideas how to make this work?

Last edited by Scott; 06-16-2010 at 01:46 PM.. Reason: Please use code tags
# 2  
Old 06-16-2010
Hi.

Why are you testing this (as it looks like you're trying to):

Code:
&& [ php /pathtoscript/parse_script.php ]

and not just running it?
Code:
&& php /pathtoscript/parse_script.php

You may also need to provide the full path to the php binary.
# 3  
Old 06-16-2010
Traditional way is to have the script create a file somewhere containing the process' PID, and check if the file's present and the PID's valid to see if it's still running. The start-stop-daemon utility can help simplify this.

Your way may work if you're only ever running one instance at a time, ever. You've got some extra [ ] there. Try this:
Code:
[ -z "$(ps ax|grep /pathtoscript/parse_script.php|grep -v grep)" ] && php /pathtoscript/parse_script.php

# 4  
Old 06-16-2010
I removed the extra braces. I set it to run every 2 minutes like this.

Code:
*/2 * * * * [ -z "$(ps ax|grep /pathtoscript/parse_script.php|grep -v grep)" ] && php /pathtoscript/parse_script.php

Then after a couple minutes, there are 2 jobs running. Here's what I saw in the Control Panel...

Code:
PID    Process

20010    /bin/sh -c [ -z "$(ps ax|grep /pathtoscript/parse_script.php|grep -v grep)" ] && php /pathtoscript/parse_script.php    

22072    /bin/sh -c [ -z "$(ps ax|grep /pathtoscript/parse_script.php|grep -v grep)" ] && php /pathtoscript/parse_script.php

I checked my email and saw that the command failed...

Code:
/bin/sh: fork: Resource temporarily unavailable
/bin/sh: fork: Resource temporarily unavailable
grep: write error: Broken pipe

Most of the time this cron command works, but every so often, it fails. How could I change the command so that when the command gets an error, it simply does NOT execute the php script?
# 5  
Old 06-16-2010
Wouldn't it be simpler, and more robust, to put the check (if the script is already running) in the script itself?
This User Gave Thanks to Scott For This Post:
# 6  
Old 06-16-2010
Quote:
Originally Posted by scottn
Wouldn't it be simpler, and more robust, to put the check (if the script is already running) in the script itself?
I tried it in my script but wasn't getting results from exec statement, but now I have it working...
Code:
exec("ps  ax", $results)

Thanks for pointing me in the right direction.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

How to end script in a cron job?

I've created a script to copy backup files from an HP-UX 11iv3 system to an NFS share on another machine. I want to schedule the script to run via cron. The script is simply three lines of cp /backups/Backup /shared/Backup. I've saved the script as a .sh file and call it with KSH. Do I need to... (3 Replies)
Discussion started by: jduehmig
3 Replies

2. UNIX for Dummies Questions & Answers

Submitting cron job through script

I would like to run a script, as root, which will eventually set up cron job for a non privilege user. Please advice. (9 Replies)
Discussion started by: atanubanerji
9 Replies

3. UNIX for Dummies Questions & Answers

Error while running a script through cron job

Hi Team, When i am running the below query manually it is giving me the right output i.e. export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH ADMIN=abc@abc.com CPU_HIGH=`sar|awk '{print $9}'|sort -n|head -5|sed -n 5p` CPU_MAX=`echo "scale=3; 100-$CPU_HIGH" | bc` CPU_LOW=`sar|awk '{print... (13 Replies)
Discussion started by: Ekamjot
13 Replies

4. Solaris

cron job for phython script

Hello, How do I schedule a cron job for a phython script to run every hour? Also, in case in future I decide to edit/cancel the job how should i do it? Does it matter where my phython script is located? Also, I have am using mailx utility in my script to send me an email and dont want... (7 Replies)
Discussion started by: siddhans
7 Replies

5. UNIX for Dummies Questions & Answers

cron job for the created shell script

Hi am newbie for unix shell.. how to create a cron job for my already created shell script.:confused: Thanks! (1 Reply)
Discussion started by: vidhyaS
1 Replies

6. Shell Programming and Scripting

Running script file using cron job every 5 second

Hi All, I beginner in unix, i have no idea how to set the script file using cron job every 5 second. I also want to execute automatically the output to text file.This is my script name countsys.sh and my textfile abc.txt. (6 Replies)
Discussion started by: mastercar
6 Replies

7. Shell Programming and Scripting

Cron job shell script..

Hey Guys, i was trying out a shell script which has to remove a file for every 90 mins. this is the code i came up with . $ crontab -e file1 file1 contains 30 1 * * * * rm -r /folder1/folder2/somefile.txt Now i need the cron to run for every 90 mins. the problem with this is... (8 Replies)
Discussion started by: Irishboy24
8 Replies

8. UNIX for Advanced & Expert Users

Need help with a script run by a cron job.

Hi, new to this forum and not sure if this is the right place to post. I'm new to cron jobs and scripts, and I need some help with a script to be used with a cron job. I already have a bot set up at a certain website address and need a script that the cron job will load every day that tells it to... (1 Reply)
Discussion started by: klawless
1 Replies

9. Shell Programming and Scripting

script wont run from cron job

I have written a simple bash script that will run a wget command to recursively ftp an entire directories and it's contents. #!/bin/bash wget -r -N ftp://user:pass@server//VOL1/dir If I run from the regular command line it works fine. root@BUSRV: /media/backup1$ ./gwbu When I put it in... (1 Reply)
Discussion started by: mgmcelwee
1 Replies

10. UNIX for Advanced & Expert Users

Cron job for Perl script

Although there are many threads on this forum regarding cron, none have specifically answered my question. So hopefully someone can shed some light on what I'm doing wrong.. I have a perl script that I want to run in a cron job. Since I've read that cron doesn't have any environments set, I... (3 Replies)
Discussion started by: man
3 Replies
Login or Register to Ask a Question