LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-05-2019, 08:46 AM   #1
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Question Best practice to make sure cron doesn't launch a task already launched by anacron


Hello,

I want to use both anacron and cron but with priority given to cron because I would prefer that my specific task is triggered at a specific time.

So let's assume my script to be triggered is /root/myTask.sh.
I want it to run at 3.00 am daily.
As a backup, if my laptop was off or something, I want anacron to launch it ASAP.

Here is my implementation:
Quote:
Originally Posted by /etc/crontab
0 3 * * * /root/myTask.sh; anacron -u myTask
Quote:
Originally Posted by /etc/anacrontab
@daily 20 myTask /root/myTask.sh
/root/myTask is executable

Everything should work as expected EXCEPT if my laptop reboots between 0.00 and 3.00 because anacron will launch myTask AND then cron as well at 3.00.

What is the best practice to make sure cron doesn't launch a task already launched by anacron please? Is it by creating a kind of flag/file and checking its value by cron?

Thank you in advance
 
Old 09-06-2019, 01:01 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I don't think there is a "best practice" for it.
Most probably I would try to check if it was already running to avoid second execution.
 
Old 09-06-2019, 04:52 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
A flag would work or have the cron job check the anacron timestamp /var/spool/anacron.
 
Old 09-06-2019, 09:40 AM   #4
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Your best option is to set a flag when one of the two methods, cron or anacron launches the application. The flag should include a timestamp and a sequence number and an identification as to which of cron/anacron did the set.

If your laptop was off for a weekend, and you happen to start the laptop coincident with the crontab entry timestamp, you will encounter a problem. One way to circumvent such situations is to not get into them.

Using the who, and the sequence number, and date, you should be able to resolve problems due to long passages of time.

It might help to add a sleep command to the anacron launched application the "job" will have to wait for the sleep command to expire. In that time interval, it could be that the crontab entry will launch and complete. The sleep command is to prevent race conditions.
 
Old 09-06-2019, 10:29 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
The OP's anacron entry has a 20 minute delay which means the only time there could be a problem is if the laptop starts at ~0240. The cron and anacron job could also check in addition to a flag/timestamp if there is an existing mytask running process.

I assume that it is working but system cron jobs i.e. /etc/crontab should have a user name, never tried without so I guess it defaults to root.
Quote:
0 3 * * * root /root/myTask.sh; anacron -u myTask
 
Old 09-06-2019, 10:45 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You could make use of a lock file, as has been said, have either of your reboot processes create a file /var/tmp/BOOT_LOCK and then check if the file is more than a period of time old and only reboot if it's older than expected. The code example below is modified from a production script snippet that will delete a lock file that's more than 4 hours old but will exit if there's a lock file that's under 4 hours old.

Code:
# We just process the lock file right at the start, because if it's here then we really don't
# care about anything else, we're just going to exit quietly

# This is a bit funky, we see if the lock file has been modified in the last 4 hours.  If the
# mtime is more than 240 then it's not been modified so we delete it.
find /var/tmp -mmin +240 -name "BOOT_LOCK" -delete

# Ok, so part of me wants to check the $? result of the /find to see if a file was deleted, that would
# be the much more elegant way but we'll just go retro / "old school" on this file's ass.

# If the lock file exists then we just exit doing nothing.
if [[ -f /var/tmp/BOOT_LOCK ]] ; then
  echo "--------------------"
  echo $(date) Lock File Found
  ls -l /var/tmp/BOOT_LOCK
  # Tiny Elvis Has Left The Server
  exit 1
fi

# So now if we get here there's a no lock file, we can start thinking about rebooting

touch /var/tmp/SMS_LOCK
sudo shutdown -r now

# Elvis Has Left The Server
 
  


Reply

Tags
anacron, cron, timing



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
LXer: YubiKey 5 Series Launched, Google Chrome's Recent Questionable Privacy Practice, PlayOnLinux Alpha Version 5 Released, Android Turns T LXer Syndicated Linux News 0 09-25-2018 06:32 AM
Learning cron : CentOS7 : anacron and cron fanoflq Linux - Newbie 2 11-24-2016 09:50 PM
LXer: What is Anacron and usage of Anacron in Linux LXer Syndicated Linux News 0 01-14-2015 06:30 AM
LXer: What is Anacron and usage of Anacron in Linux LXer Syndicated Linux News 0 01-14-2015 02:00 AM
Can I use "anacron" to launch GUI app (PerlTk)? kaza Linux - Software 1 01-07-2012 08:24 PM

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

All times are GMT -5. The time now is 10:59 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