LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script per cronjob executed several times (https://www.linuxquestions.org/questions/linux-newbie-8/script-per-cronjob-executed-several-times-947716/)

the_bigbalu 05-31-2012 03:21 AM

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?

catkin 05-31-2012 03:25 AM

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.

the_bigbalu 05-31-2012 03:42 AM

Hi,

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

the_bigbalu 05-31-2012 06:30 AM

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


catkin 05-31-2012 07:17 AM

Puzzling. Do those processes have children?

pan64 05-31-2012 07:19 AM

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

michaelk 05-31-2012 07:31 AM

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

catkin 05-31-2012 07:59 AM

Quote:

Originally Posted by pan64 (Post 4691967)
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"
}


unSpawn 05-31-2012 01:57 PM

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; }

suicidaleggroll 05-31-2012 05:37 PM

Quote:

Originally Posted by michaelk (Post 4691972)
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.

the_bigbalu 06-01-2012 01:14 AM

Hi,

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


All times are GMT -5. The time now is 08:00 PM.