overlapping cron job?

JMaster

Golden Member
Feb 9, 2000
1,240
0
0
I'm writing an hourly cron job that is sending files to a remote server via FTP. Problem is, some of these files may take over an hour to send, so I don't want the cron job to be run if an instance of itself is already running. Is this possible to check?
 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
uh a trivial solution would be to touch a file in /tmp at the beginning of the script and remove it when it's done. only execute the ftp logic if the temp file is not there.
of course it has it's holes but if you want something quick and painless...

you can also grep for your script in ps
 

JMaster

Golden Member
Feb 9, 2000
1,240
0
0
Originally posted by: akubi
you can also grep for your script in ps
that's what I was thinking, but is there a way to specify which process is the current cron job?

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
The flag file is probably the better solution, parsing ps output can be really ugly.
 

juiio

Golden Member
Feb 28, 2000
1,433
4
81
Here is something you can use...

homedir="/home/whatever"

processpid=`cat $homedir/process.pid`

cd $homedir

if [ "$(ps x|grep $processpid|grep -v grep)" = "" ]; then

DO COMMAND HERE
echo $! > $homedir/process.pid

fi
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
If it were me, I'd do something like this:

if[-x /tmp/ftp.lock]
else
{touch /tmp/ftp.lock
*script goes here*
rm /tmp/ftp.lock}
 

TGS

Golden Member
May 3, 2005
1,849
0
0
We use the "lock" file methodolgy in our scripts. More or less the same thing between nothin and terrys options. Easier to explain to other people what the purpose is, and self-explanitory for people looking at the file.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: TGS
We use the "lock" file methodolgy in our scripts. More or less the same thing between nothin and terrys options. Easier to explain to other people what the purpose is, and self-explanitory for people looking at the file.

:thumbsup:
In fact, all the hard stuff is already done for you: man lockfile
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: TGS
We use the "lock" file methodolgy in our scripts. More or less the same thing between nothin and terrys options. Easier to explain to other people what the purpose is, and self-explanitory for people looking at the file.

Bingo, only reason I prefer to write my own is sometimes I'm on systems where lockfile isn't installed/ported and it's just easier to write it out.

Plus, after the second or third it becomes second nature.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: TerryMathews
Originally posted by: TGS
We use the "lock" file methodolgy in our scripts. More or less the same thing between nothin and terrys options. Easier to explain to other people what the purpose is, and self-explanitory for people looking at the file.

Bingo, only reason I prefer to write my own is sometimes I'm on systems where lockfile isn't installed/ported and it's just easier to write it out.

Plus, after the second or third it becomes second nature.

The hand-rolled solutions have a potential race condition - if a second process checks for the lock after the first process checks, but before the first process creates the file, you'll end up with problems.

I've always assumed that lockfile handles this somehow. Or am I just delusional? In any case, it has some other nice features like retries, forcing removal after some specified time, etc.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Unless your process dies unexpectedly without deleting the file...

True, but that probably indicates bigger issues that will require manual intervention anyway.

The hand-rolled solutions have a potential race condition - if a second process checks for the lock after the first process checks, but before the first process creates the file, you'll end up with problems.

Unless cron freaks out that won't be possible since the job is hourly.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Nothinman
Unless your process dies unexpectedly without deleting the file...

True, but that probably indicates bigger issues that will require manual intervention anyway.

The hand-rolled solutions have a potential race condition - if a second process checks for the lock after the first process checks, but before the first process creates the file, you'll end up with problems.

Unless cron freaks out that won't be possible since the job is hourly.

Yep - for this particular case. And in fact anything kicked off by cron since it only checks every minute. Unless you maliciously schedule the jobs simultaneously :evil:

I use it for some other stuff that can be launched at random intervals - so it's possible, but very unlikely.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |