LinuxQuestions.org
Register a domain and help support LQ
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, 04: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, 04:25 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

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

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Puzzling. Do those processes have children?
 
Old 05-31-2012, 08:19 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
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 08:21 AM.
 
Old 05-31-2012, 08:31 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,938

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

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 02:57 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
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, 06:37 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 02: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:21 AM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 06:27 AM
CGI executed 2 times when form is posted lhought Linux - Enterprise 2 10-13-2006 08:21 AM
# of times loops are executed leroy27336 Programming 10 09-14-2004 05:07 AM
hotplug script executed 3 times Borelian Linux - Software 1 06-29-2004 12:46 AM


All times are GMT -5. The time now is 04:07 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration