Backup script / Test if script is already running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Backup script / Test if script is already running
# 1  
Old 05-22-2012
Backup script / Test if script is already running

Hello everyone,

I have 2 questions :

1) I have a backup shell script, let's call it backup.sh, that is called every hour as a cron job.
As a matter of fact a backup could last more than one hour.
It mounts a NAS and then do some rsync on important directories, so really I don't want to start the srcipt again if the previous backup is not finished.
In order to test if the script is not already running, I am doing this :

if [ `ps-ef | grep -c backup.sh` -eq 1 ] # backup.sh is not already running
then
# execute backup.sh
fi

Is it the best way to do this test?
Do you have any suggestion for improvement?

2) Could you please share or point out where I could find some well thought backup script?

Many thanks for your help and keep up the good work!
freddie50
# 2  
Old 05-22-2012
A traditional way to tell if a service is already running is a PID file. Save the process ID in a temporary file and delete it on exit. If the PID file exists, and the PID is still valid, then another backup is running. Even if the process dies unexpectedly and doesn't delete, it should be able to cope since the odds of something else snapping up the same PID are small.

This is how services often work and much more reliable than trying to grub through ps's own text output for answers.

Code:
#!/bin/sh

if [ -f /tmp/myservice-pid ]
then
        read PID < /tmp/myservice-pid

        # Check if the PID in the file still exists
        # If it does, backup is running, and we should skip this round.
        if ps "$PID" >/dev/null 2>/dev/null
        then
                echo "Already running" >&2
                exit 1
        fi
fi

# Either create new file, or replace it with our own
echo $$ > /tmp/myservice-pid
# Delete it on exit just to be extra sure
trap "rm -f /tmp/myservice-pid" EXIT

# Continue below

...

# 3  
Old 05-22-2012
Many thanks for your reply that is brilliant and very useful!
Thanks again,
freddie50
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

3. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

4. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

5. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

6. Shell Programming and Scripting

Help: Bash backup script (includes copy, test-

Basically it's for a work assignment. Have to make a menu with the following choices ***************menu********************* 1) Show Current Directory 2) Dispaly Current Time and Date 3) Copy 4) Change Password 5) write directory to file 6) Edit File Directory 7) Make backup from... (1 Reply)
Discussion started by: Covax
1 Replies

7. Shell Programming and Scripting

script for reading logs of a script running on other UNIX server

Hi, I have a script, running on some outside firwall server and it's log of success or failure is maintained in a file. I want to write a script which ftp that server and reads that file and checks the logs and if failure , I will send mail notification. Please let meknow if I am not... (1 Reply)
Discussion started by: vandana.parwani
1 Replies

8. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

9. Shell Programming and Scripting

Script to test for scripts running

Hi, Im writing a script that will check which scripts are running. The script will run on a 5min loop and the status of the scripts will be written to the log. If any of the scripts arent running an email will be sent out. At the min if all scripts are running an entry is made to the log... (19 Replies)
Discussion started by: runnerpaul
19 Replies

10. UNIX for Dummies Questions & Answers

Script to Test Application Server is running

Hi, I'm a complete novice at Unix and need to create a script that does the following... checks to see if an application server is running. If the app is running then print 'Available' Else print 'Unavaliable' exit from scriopt I have no idea where to start. I'd be very grateful... (0 Replies)
Discussion started by: duglover
0 Replies
Login or Register to Ask a Question