Double Check my Cron Job


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Double Check my Cron Job
# 1  
Old 04-01-2013
Double Check my Cron Job

Hello All,

I was wondering if someone could take a quick glance at this Cron job and tell me if it's going to do what I am expecting.

I want it to run everyday at 8:00 am and 5:00 pm.

And the line in Crontab looks like:
Code:
0 8,17 * * * /usr/local/myScript.sh

Does that look correct?

Thanks in Advance,
Matt
# 2  
Old 04-01-2013
OK

Looks like the correct format.
This User Gave Thanks to joeyg For This Post:
# 3  
Old 04-01-2013
Hey Joey, thanks for the quick reply!

Cool, thanks for taking a look. Its a pretty important server so I just wanted to make sure it was correct before I put it into effect...

Thanks Again Much Appreciated,
Matt
# 4  
Old 04-01-2013
Just a few suggestions:

1. Your script should definitely not reside in "/usr/local", but in "/usr/local/bin". See the File System Hierarchy Standard, which is a very good guideline for what to place where.

2. Have you made sure you do not fall for the Cron Problem Number One?

3. You should redirect <stdout> and <stderr> in your cron line, either to a log file or to "/dev/null". Otherwise you will get strange mails to the user account the cron job runs under. Modify your line to something like this:

Code:
0 8,17 * * * /usr/local/bin/myScript.sh 1>/dev/null 2>/dev/null

Corollary: you might ask yourself why i did not use "> /dev/null 2>&1". First, i find this easier to read. Second, this will do the same regardless of order, while "2>&1 > /dev/null" will not do the same when written this way or the other way round.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 04-02-2013
Hey bakunin, thanks for the reply.

Sorry I didn't put my actual path in there I was just throwing something in there as a placeholder more or less...

The script is actually located at "/usr/local/nagios/libexec/...."
Was the "Cron Problem Number One" page just saying don't use relative paths, use absolute? If that's the case then I'm ok there.

Also, my script doesn't have any real STDOUT. The script interacts with the Nagios' NSCA Daemon which works with the
Nagios Host Server, by sending some output in a specific format to the Nagios Server for processing.

What my script does is write its output to a txt file, then while still in the script I execute the "send_nsca" Command which
is what actually sends the contents of the txt file to the Nagios Server...

I put those cron jobs into effect yesterday before I left work and added a line in my script temporarily (which I removed this
morning)
that wrote the Date/Time and which option was used (because there a 5 commands in cron, same script for all,
but each with a different CLI Option)
to a makeshift log file to see if everything was executed at the correct times and what not,
and when I checked this morning the times and CLI Options matched perfectly with what I wanted it to do!


Thanks Again guys for all your tips, the info is much appreciated!


Thanks Again,
Matt

Last edited by mrm5102; 04-02-2013 at 12:01 PM..
# 6  
Old 04-02-2013
Quote:
Originally Posted by mrm5102
Was the "Cron Problem Number One" page just saying don't use relative paths, use absolute? If that's the case then I'm ok there.
Actually this is a small part of it. The basic problem is: a scirpt started from commandline inherits the environment of the login shell, but a script started from cron inherits the environment of "init", which is next to empty. Guess what happens to heirs of poor processes? Sometimes they go broke, so to say. ;-))

This usually lands here in the form of: "my script worked perfectly from commandline, then i put it in a crontab and since then i doesn't work any more". We have these threads about once a week, which is why i started to call this "Cron Problem Number One". As you relate that your script works this seems not to be the case, but that was not clear from the start so i thought i'd mention it to put it on your checklist.

Regarding the suggested redirections: you just need to have some error condition and some command will write something to stdout. I would redirect any job in cron in the way i mentioned, just to be sure.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 04-03-2013
Hey Bakunin, thanks for the reply!

No worries, I'm always willing to take some constructive criticism...
Thanks for the info about cron, didn't know that (that it inherits the environment from init).

I'll be sure to add some logging stuff to the script and have it redirected as suggested.


Thanks again for the help, very much appreciated!


Thanks Again,
Matt
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

2. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

4. Shell Programming and Scripting

check mail backup cron job

i have just installed a cron job to run at 3 in the morning once a week. It's difficult for me to test if it works because the email backup program takes the email service down before running the backup. I wondered if i could ask you gurus if this would work? 0 3 * * 0 ... (1 Reply)
Discussion started by: timgolding
1 Replies

5. Shell Programming and Scripting

Cron Job help

I need to write the cron job for the following scenario, Please help me out The CRON job runs sometime at night on Saturday and checks if there are more than eight files in the /PROCESSED folder. Any files over and above eight are deleted based on ascending order of date (eight most recent... (10 Replies)
Discussion started by: sandeep.dwivedi
10 Replies

6. UNIX for Dummies Questions & Answers

cron job

Hi, How to monitor whether a cron job is running or errored out..?other than checking the process using ps-aef how to enter the cron job which throws the output in 1 file and errors in other file. i remember it can be done using >1 and >2 ..but not sure.. any expert..please help!! (1 Reply)
Discussion started by: rujus
1 Replies

7. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

8. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies

9. Shell Programming and Scripting

cron job

I believe that crob invokes command under bash. But most of our work are carried out using c shell. So what do I need to be aware of (or what precautions should I be taking) when I use cron. (1 Reply)
Discussion started by: sharanbr
1 Replies

10. UNIX for Dummies Questions & Answers

Cron job

Regarding Cron job, i want to execute a scriptevery 4 days once irrespective of a week or a month. Is it possible?Any help (2 Replies)
Discussion started by: santho
2 Replies
Login or Register to Ask a Question