M
M
Maxim2014-08-19 20:01:50
linux
Maxim, 2014-08-19 20:01:50

How to prevent double script execution?

Wrote a simple script for database backup

#!/bin/bash

BACKUP_PATH="/home/backup"
USER="user"
PASS="xxxx"
LOGFILE="/var/log/backup.log"

echo "[--------------------------------[`date +%F--%H-%M`]--------------------------------]" >> $LOGFILE
echo "[----------][`date +%F--%H-%M`] Run the backup script..." >> $LOGFILE

mysqldump -u $USER -p$PASS s11 >> $BACKUP_PATH/s11-`date +%F--%H-%M`.sql

echo "[----------][`date +%F--%H-%M`] done." >> $LOGFILE

exit

If run manually, it's fine. if it is launched by cron, then it is launched twice and in parallel scribbles the base into a file.
34 20 * * * /home/db.sh
tell me where to look. Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2014-08-19
@maxpointn2point

Run the script via flock :
From the advantages over just creating a lock file and checking it when running the script: the situation when the process was nailed or segfaulted by something, that is, the lock file remained, but the process was not started. In this case, it will not start until you delete the lock file manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question