I want to programatically change the hour which certain cron jobs execute.
I'm not sure whether I'm going about this the right way, but here's the plan.
Jobs which are subject to change will have a comment on the end of the line "#change-enabled".
15 5 7,14,21,28 * * /path/to/executable1 #change-enabled
15 * * * * /path/to/executable2
45 5 */2 * * /path/to/executable3 #change-enabled
Then I want to pipe the output from "crontab -l" through sed to catch and alter jobs with that comment on the end, and pipe the results of that through "crontab -".
// Edit
My gut tells me to stay away from doing this as root because the new hour is determined using data from a 3rd party. (Google Analytics Data Export API) So /etc/cron.d/ is something I'm going to stay away from for this application.
Here is the command which has been working with all of my current cron jobs during debugging. I've been using "date +%l" in place of "/home/user/slow-hour" and leaving off the "| cron -" to see what my gets printed.
crontab -l | sed -e "s/^\([^ ]*\) [0-9]* \(.*#change-enabled\)/\1`/home/user/slow-hour` \2/" | crontab -
The contents of ~/slow-hour will be a script which fetches hourly data for website profiles at Google Analytics and looks for the slowest hour. In the event of any error whatsoever that script will return 1AM, mail me a notice and then bail.
In order the reduce the likelihood of a days execution being skipped due to the slow hour being earlier than the current hour, the automated cron rescheduler will be set to run at midnight and the earliest hour ~/slow-hour will return will be 1AM.
Based on my manual cron job adjustments in the past, I don't anticipate excluding midnight from being the slowest hour being a problem in my case.
crontab -l | sed '/change-enabled$/s/pat/repl/' | crontab -
Assuming you want to set the hour to 8, one command would be
sed -e 's/\([0-9]*\) [0-9]* \(.*#change-enabled\)/\1 8 \2/'
You can do that, but there's a better way (provided, you've got root access).
Instead of altering crontab, split jobs into related groups. For each group put a new file in /etc/cron.d/{jobname}. This way you can just regenerate one script without problems - no sed magic needed. Split everything that can change into separate files, so that you just write a new file in place of the old one and don't mess with the entries that don't need changing.
The only difference is the syntax. Instead of:
<time> <command>
you have to put there:
<time> <user> <command>
so that the job is run with the correct rights.
Example - instead of 'crontab -l | sed -e 's/....somejob.../.../ | crontab ..' just run 'echo .... > /etc/cron.d/somejob'
crontab -l | sed -re '/# *change-enabled *$/s/^([^ ]+) [^ ]+/\1 new_hour/' | crontab -
Related
What is the most optimal approach for creating a crontab for a specific user, as part of a Packer build process? Much of what i've seen covers using the crontab utility as a text editor, and does not specify using pipes or output to load the file. This operation would be run from sudo, since the cron directories are root-owned directories.
The simplest way to "import" file in to cron record is to use command like this:
crontab input_file
the input file should contain records on the same way they are stored in cron files, something like
1 2 3 4 5 /path/to/executable
Assuming that you have your script copied to /home/ec2-user/scripts/test.sh, below one liner will setup the crontab to run the script. We use this in our Packer shell provisioner to setup the crontab entries. Below sample entry runs every minute. You can change the timing.
(crontab -l 2>/dev/null; echo "* * * * * /home/ec2-user/scripts/test.sh") | crontab -
I'm digging along CRON and scheduling.
I setup a scheduled job to fire every minute via $crontab -e + editing the file (weirdly named "/tmp/crontab.vst6TX/crontab")
My understanding is that $crontab -e opens A crontab... and that cron.d, the daemon, picks up the crontab and APPENDS the cron job within to the (systemwide) /etc/crontab. (as per comment from crontab being saved in tmp/ in debian)
I'm watching the cron job fire every minute - yet I can't see it being added to the /etc/crontab job list... why? $crontab -l does show the job...
crontab -e and crontab -l are to edit and display (respectively) the current user's crontab file (which are physically located in /var/spool/cron/crontabs). Therefore, each user can have their own separate crontab file in that directory. So when you ran crontab -e and added a cron line, you ran crontab -l as the same user presumably, and therefore saw the line you added.
/etc/crontab is a completely different file. You are correct, it is systemwide -- notice that the cron lines in that file specify a user. The same is true for files in /etc/cron.d, the cron lines in the files will specify a user.
Oh and also, the .d suffix in cron.d does not refer to daemon. Check this post out.
I have a file which I loaded into crontab with the following jobs:
# script1 executes at 12:30 daily
# script2 executes at 12:35 daily
30 12 * * * /usr/bin/wget -q -O temp.txt http://<host-url>/cronjob/script1.php
35 12 * * * /usr/bin/wget -q -O temp.txt http://<host-url>/cronjob/script2.php
I followed the rules as per this site:
How to Set-up a Cron Job
I initially had an issue with having both jobs display in the crontab list. I resolved that issue by having all jobs set up on one line as detailed in the section of:
Dealing with Error Messages from Crontab
Once I had both jobs listed correctly, I tested to verify that they executed correctly. At 12:31 I noted that both scripts executed.
Why did this occur? How can I schedule it so that script2 executes at it's own scheduled time?
I realize this is not such a huge issue but I am curious to know.
Thank you for your assistance.
I opened the cron job to be edited using:
crontab -e
And made edits using Nano.
I added a new line character between jobs and this fixed the problem.
So the file initially created was done on a Windows machine which was FTP to the server. I checked out the file and it was created in Notepad++ without an extension, also tried with an extension of .TXT. The EOL character was set to UNIX.
No matter the scenario, when loading the file, both jobs executed on the first scheduled time. After I modified cron jobs using the edit above. The cron jobs executed at the scheduled time (aka 5 minutes apart).
I've got about a dozen servers that each have crontabs with anywhere from 20-50 crontab entries. My single most common cause of a process failure is someone commenting out jobs in cron during a fix or patch and then forgetting to uncomment the jobs.
I'd like to do two things to solve this:
Start using our schedule suppression process that allows users to suppress schedules without actually touching crontab. Nothing magical - just touch a file in a directory dedicated to the process. The process checks that directory on start-up.
Implement a process that will send out alerts if crontab doesn't match its backup or current version in svn.
Can anyone recommend an existing solution for #2 (alert when crontab changes)?
In this case i would suggest to compare hashvalues of the file you want to have and the actual file.
Just write a little bashscript that sends out a emailnotification or creates a notification file or whatever you want and let this script be run automatically every x seconds / minutes / hours.
A possible script could be
if [[ $(md5sum path/to/crontab.backup | cut -d' ' -f1) == $(md5sum /etc/crontab | cut -d' ' -f1) ]]
then
# send your notification
fi
This is a very simple solution to check if a file was changed since the last backup was made.
this is my first time writing script for cron job.
I wrote my code in shell, (which it works) and I'm trying to set it up for cron.
So here is my question. How do I set up the cron? Am I suppose to write
10 * * * * /home/workstation/deleter.sh (I want it to run every 10min)
right underneath #!/bin/sh? How would I execute it? (deleter.sh has permission via chmod)
man 1 crontab returns "No entry for crontab in section 1 of the manual"
I'm really lost and confused right now. If someone know how to set up cron please tell me!!
Thanks in advance
#!/bin/sh
counter=0
logloc=/home/ServerLogs
backup=/home/test
## Reads the location of the file systems that needs to be investigated from location.txt
## and save it into an array
while read -r line; do
Unix_Array[${counter}]=$line;
let counter=counter+1;
done < location.txt
## Reads Email recipients and save it into an array
More code continues from here......
The following will open your environment's text editor and load the crontab:
crontab -e
Your crontab entry is mostly correct. In order for your script to run every ten minutes it should be changed to:
*/10 * * * * /home/workstation/deleter.sh
The entry you indicated would run the script at the 10th minute of every hour.
To setup the cron, you can do one of two (main) things. The first would be to place the specified line in /etc/crontab. The second would be to run crontab -e and place the line in there. I would recommend to use crontab -e so the cron will execute as your own user account.
If the full path to the script is /home/workstation/deleter.sh and it does have execute-privileges, as you specified - your current line will have it execute 10-minutes past the hour, every hour. To get it to execute every 10 minutes, you'll have to use */10, like this:
*/10 * * * * /home/workstation/deleter.sh
this might help
http://www.manpagez.com/man/5/crontab/
you need to get an entry into your crontab
One of the best links I came across when I first learned about cron! Bookmark it
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/