Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-31-2012, 03:21 AM
|
#1
|
LQ Newbie
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21
Rep: 
|
script per cronjob executed several times
Hi there,
I made a script for copy files to an directory. It looks like:
Code:
date=`date +%F`
sdir=/home
backupdir=/mnt/backup
ddir=$backupdir/$date
mkdir $ddir
rsync -avz $sdir $ddir
#Packe die Daten um Festplattenplatz zu sparen
if [ $? = 0 ]
then
tar cvjf $ddir.tar.bz2 $ddir
rm -rf $ddir
else
exit 50
fi
When i run this script, one task is running. If i put this into crontab, there a 50 copyjobs und 50 packagejobs. What can i do to run this script on a time (cron) and only one task?
|
|
|
05-31-2012, 03:25 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
What is the cron job line? There is nothing in the script to create 50 instances so it must be the cron job that does so.
|
|
|
05-31-2012, 03:42 AM
|
#3
|
LQ Newbie
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21
Original Poster
Rep: 
|
Hi,
the Cronjob looks like this:
* 23 * * * /root/script/backup.sh
Last edited by the_bigbalu; 05-31-2012 at 06:39 AM.
|
|
|
05-31-2012, 06:30 AM
|
#4
|
LQ Newbie
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21
Original Poster
Rep: 
|
after the cronjob started, it looks like:
Code:
root@server:~# ps aux | grep backup
root 25540 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25575 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25603 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25656 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25680 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25710 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25743 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25778 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25814 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25874 0.0 0.0 2556 1036 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25921 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25939 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 25973 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26005 0.0 0.0 2556 1036 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26029 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26070 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26111 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26143 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26190 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26216 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26245 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26274 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26302 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26362 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26386 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26443 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26465 0.0 0.0 2556 1028 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26505 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26531 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26575 0.0 0.0 2556 1044 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26603 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26646 0.0 0.0 2556 1032 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26671 0.0 0.0 2556 1020 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26723 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26765 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26786 0.0 0.0 2556 1016 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
root 26815 0.0 0.0 2556 1040 ? Ss May30 0:00 /bin/bash /root/script/backup.sh
|
|
|
05-31-2012, 07:17 AM
|
#5
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Puzzling. Do those processes have children?
|
|
|
05-31-2012, 07:19 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,485
|
I would make a pid file (script creates and removes at the end).
When it exists script will do nothing, just exit.
also you can add set -xv and redirect output (and stderr) to a logfile to see what's happening
Last edited by pan64; 05-31-2012 at 07:21 AM.
|
|
|
05-31-2012, 07:31 AM
|
#7
|
Moderator
Registered: Aug 2002
Posts: 26,856
|
Quote:
* 23 * * * /root/script/backup.sh
|
The * in the minute column means it will run every minute between 2300 - 2359. I would expect to see 60 jobs but it depends on how long it takes to perform the backup.
This will run your job once.
00 23 * * * /root/script/backup.sh
Last edited by michaelk; 05-31-2012 at 08:01 AM.
|
|
|
05-31-2012, 07:59 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by pan64
I would make a pid file (script creates and removes at the end).
When it exists script will do nothing, just exit.
|
Here's a simple function to implement that (assumes $pidfile contains the name of the PID file)
Code:
# Check and write PID file
function PIDfile {
local cmd my_name PID
if [[ -f "$pidfile" ]]
then
my_name="${0##*/}"
PID=$(cat "$pidfile")
cmd=$(ps --pid "$PID" --format cmd --no-headers)
if [[ "${cmd##*/}" = "$my_name" ]]
then
echo "$my_name is already running with PID $PID" >&2
exit 1
else
echo "Refreshing stale PID file, $pidfile" >&2
fi
fi
echo $$ > "$pidfile"
}
|
|
|
05-31-2012, 01:57 PM
|
#9
|
Moderator
Registered: May 2001
Posts: 29,417
|
This doesn't cover every aspect but stops the script in one instead of seventeen lines ;-p :
Code:
[ $(pgrep -f /root/script/backup.sh|head -1) -eq $$ ] || { logger "Already running, exiting."; exit 0; }
|
|
1 members found this post helpful.
|
05-31-2012, 05:37 PM
|
#10
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by michaelk
The * in the minute column means it will run every minute between 2300 - 2359. I would expect to see 60 jobs but it depends on how long it takes to perform the backup.
This will run your job once.
00 23 * * * /root/script/backup.sh
|
*ding* *ding* *ding*
We have a winner
Although making/checking a PID is a good idea as well.
|
|
|
06-01-2012, 01:14 AM
|
#11
|
LQ Newbie
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21
Original Poster
Rep: 
|
Hi,
merci for those good answers! :-) Thank you very much! 
|
|
|
All times are GMT -5. The time now is 10:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|