LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-31-2012, 03:21 AM   #1
the_bigbalu
LQ Newbie
 
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21

Rep: Reputation: Disabled
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?
 
Old 05-31-2012, 03:25 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 05-31-2012, 03:42 AM   #3
the_bigbalu
LQ Newbie
 
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21

Original Poster
Rep: Reputation: Disabled
Hi,

the Cronjob looks like this:
* 23 * * * /root/script/backup.sh

Last edited by the_bigbalu; 05-31-2012 at 06:39 AM.
 
Old 05-31-2012, 06:30 AM   #4
the_bigbalu
LQ Newbie
 
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-31-2012, 07:17 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Puzzling. Do those processes have children?
 
Old 05-31-2012, 07:19 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,018

Rep: Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342
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.
 
Old 05-31-2012, 07:31 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,776

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
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.
 
Old 05-31-2012, 07:59 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by pan64 View Post
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"
}
 
Old 05-31-2012, 01:57 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
Old 05-31-2012, 05:37 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by michaelk View Post
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.
 
Old 06-01-2012, 01:14 AM   #11
the_bigbalu
LQ Newbie
 
Registered: Jun 2011
Location: Bavaria
Distribution: Fedora 18, Ubuntu 12.04
Posts: 21

Original Poster
Rep: Reputation: Disabled
Hi,

merci for those good answers! :-) Thank you very much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
When was the script last executed? AnAsianCalledRusty Linux - Newbie 1 01-18-2011 10:21 AM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 05:27 AM
CGI executed 2 times when form is posted lhought Linux - Enterprise 2 10-13-2006 07:21 AM
# of times loops are executed leroy27336 Programming 10 09-14-2004 04:07 AM
hotplug script executed 3 times Borelian Linux - Software 1 06-28-2004 11:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration