which one is better Run by Cron or Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting which one is better Run by Cron or Shell
# 1  
Old 01-03-2009
which one is better Run by Cron or Shell

Dear Experts,

I have a script defined in Cron which runs every 1 minute.

Code:
* * * * * /export/home/myscript.sh >/dev/null 2>&1

Now the issue is executing that myscript.sh sometime will take 2/3 or more minutes to finish execution.

But after 1 minute cron will invoke another instance(process) execute the myscript. In that case do you think there is a probability to overlapping the instance and may create problem.


If there is a chance for overlapping may be the below shellscript(run it in background) is okay which will run every 1 minute. Script will invoke different instant after every minute.

Code:
#!/usr/bin/bash
while :
do
    sleep 60 & pid=$!
    /export/home/myscript.sh
    wait $pid
done

Could you please make your valuable suggestion?

//purple
# 2  
Old 01-03-2009
There is another solution to this problem, but you have to add some code to your `myscript.sh'. Put some kind of lock while your program is runing, e.g. if your program is a shell script, you can add the following lines:

Code:
...
lock=/export/home/.myscript.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15
# Now lock. If lock fails, then propably the program is runing.
mkdir ${lock} || exit 0
...
Here comes the script code
...
# Now unlock, so another instance of this program can run in the future.
rmdir ${lock}

I prefer this kind of coding in such cases, because it's better to protect this kind of programs to run overlaping processes. So, either you prefer cron, or sleeping nohup shell scripts, the process is vulnerable to run in simultaneus instances; if you use some kind of locking this is impossible.
Bye...
# 3  
Old 01-03-2009
Can you try this way

#!/usr/bin/bash
while :
do
/export/home/myscript.sh
pid=$!
wait $pid
done
# 4  
Old 01-03-2009
Quote:
Originally Posted by Panos1962
Code:
...
lock=/export/home/.myscript.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15
# Now lock. If lock fails, then propably the program is runing.
mkdir ${lock} || exit 0
...
Here comes the script code
...
# Now unlock, so another instance of this program can run in the future.
rmdir ${lock}

is it just like..

Code:
#!/usr/bin/bash
lock=/export/home/.myscript.sh.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15

mkdir ${lock} || exit 0
...
Here comes my script code
...
rmdir ${lock}


After that i can put the script in Crontab.

In that case every 1 minute cron will invoke process and child process. But meanwhile, the program (myscript) can be running with different process and child process id.

right?

//purple
# 5  
Old 01-03-2009
moreover,

let say myscript.sh is running and having the below process id-

solaris:/home/user1> ps -ef|grep myscript.sh
solaris 25907 10427 0 14:36:50 ? 0:00 /export/home/myscript.sh

i put the myscript.sh in cron as 1 minute interval. So, every 1 minute interval solaris again run the myscript.sh.

DO you think solaris May try to assing the next myscript.sh with procss id 25907( which indicating overlapping)?
procss id 25907 still running.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script has different ouput via cron vs when run Manually

Hello Lads, I deployed a script on my mac to start and stop EC2 instances on AWS console. The script when started manually on the terminal does the expected stop and start. Problem is when i try to schedule it on a cron, it fails to recognize the AWS Keys which i set up as ENV variable by... (2 Replies)
Discussion started by: Irishboy24
2 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

Who -u gives different output if run from cron than if run from terminal?

If I run 'who -u' interactively or from a script invoked through bash in a tty on my Ubuntu 12LTS box I get an output like this: testuser pts/0 Dec 9 02:32 . 2163 (host.xx.yy) running the same through cron I get: testuser pts/0 2012-12-09 02:32 00:05 2163... (2 Replies)
Discussion started by: latimer
2 Replies

4. Solaris

Shell Script gives error when run through cron job.

Hi, The following shell script runs without any problem when executed manulally. USED=$(df -h /arch | tail -1 | awk '{print $5}' | cut -d '%' -f 1) if then find /arch/AUBUAT/ -type f -mtime +0 | xargs rm find /arch/AUBMIG/ -type f -mtime +0 | xargs rm fi But the same gives below... (6 Replies)
Discussion started by: ksadiq79
6 Replies

5. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

6. UNIX for Dummies Questions & Answers

shell script run by user or cron job ?

My shell script runs fine both as a cron job and when i issue it. However, I wish to differentiate when it runs as a cron-job so the "echo" statements are not issued (they get mailed to me, which i don't want). I tried checking $USER but since the cron was created in my user that does not... (5 Replies)
Discussion started by: sentinel
5 Replies

7. UNIX for Dummies Questions & Answers

cron does not appear to run

Hi everyone, I am having a problem with a cron, I am using Solaris 10 and need to run a php file every 5 minutes. I can run the file from the test user, but the cron does not seem to run. I do not see anything about it in /var/cron/log but see crons for other users running. What should I look... (10 Replies)
Discussion started by: atomicbits
10 Replies

8. UNIX for Dummies Questions & Answers

Please help me with the run-away cron jobs

I started a cron job on a cluster super computer. I am just a usual user over there and has no root power. The cron job was set by using "crontab -e". It excecutes a perl command every 20 minutes. I could use crontab -l or crontab -r to list or remove the cron job. However, I just found that I... (2 Replies)
Discussion started by: dedema
2 Replies

9. UNIX for Dummies Questions & Answers

Where does cron run from?

I have a script in the same directory as some files and directories im trying to tar up and I have it run in cron. Well it runs but says it can't find the directories, you need to be in the directory where the script is for it to work. Here is my cron and script its crappy but it does the trick =).... (3 Replies)
Discussion started by: kingdbag
3 Replies

10. Shell Programming and Scripting

Why does cron run this differently?

test.ksh =================== #!/usr/bin/ksh APPLICATION=hr_app APPLICATION_UPPER=`echo $APPLICATION | tr ` echo $APPLICATION_UPPER > /tmp/test.txt echo $APPLICATION >> /tmp/test.txt which tr >> /tmp/test.txt =================== When I run this from the shell: /home/natter> more... (6 Replies)
Discussion started by: natter
6 Replies
Login or Register to Ask a Question