LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cron job not working correctly. (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-not-working-correctly-4175486708/)

aevtech 12-03-2013 10:42 AM

Cron job not working correctly.
 
Hello all,

I tried to run a script every night at 12:00AM via a cron job. This is how I set it up in the crontab:

Code:

(root)-(jobs:0)-(~)
(! 1002)-> crontab -l

* 0 * *        * /root/scripts/systeminfo.sh

This is the script it self:

Code:

(root)-(jobs:0)-(~)
(! 1001)-> cat /root/scripts/systeminfo.sh
#!/bin/bash
echo
# This script will provide system information.
echo
echo "Here is the current system information for the server"
echo
echo "Clearing systeminfo.txt file..."
echo "" > systeminfo.txt
echo "Users Logged Into System" >> systeminfo.txt
echo >> systeminfo.txt
w >> systeminfo.txt
echo >> systeminfo.txt
echo "Server Uptime Information" >> systeminfo.txt
echo >> systeminfo.txt
uptime >> systeminfo.txt
echo >> systeminfo.txt
echo "Server Disk Space Information" >> systeminfo.txt
echo >> systeminfo.txt
df -h >> systeminfo.txt
echo >> systeminfo.txt
echo "Network Information" >> systeminfo.txt
echo >> systeminfo.txt
route >> systeminfo.txt
echo >> systeminfo.txt
echo "Memory Usage Information" >> systeminfo.txt
echo >> systeminfo.txt
free -g >> systeminfo.txt
echo >> systeminfo.txt
mail -s "System Status Report" manny@aevtech.com < systeminfo.txt
echo "End of Script"

It DOES run at 12:00AM BUT it runs every minute so I get a e-mail EVERY MINUTE. I thought it was only was supposed to run once at 12:00AM and that's it? Not sure what else I am missing for it to only run once every day at night.

Thank you for any and all help!

gacanepa 12-03-2013 10:45 AM

Quote:

Originally Posted by aevtech (Post 5074672)
Hello all,

I tried to run a script every night at 12:00AM via a cron job. This is how I set it up in the crontab:

Code:

(root)-(jobs:0)-(~)
(! 1002)-> crontab -l

* 0 * *        * /root/scripts/systeminfo.sh

This is the script it self:

Code:

(root)-(jobs:0)-(~)
(! 1001)-> cat /root/scripts/systeminfo.sh
#!/bin/bash
echo
# This script will provide system information.
echo
echo "Here is the current system information for the server"
echo
echo "Clearing systeminfo.txt file..."
echo "" > systeminfo.txt
echo "Users Logged Into System" >> systeminfo.txt
echo >> systeminfo.txt
w >> systeminfo.txt
echo >> systeminfo.txt
echo "Server Uptime Information" >> systeminfo.txt
echo >> systeminfo.txt
uptime >> systeminfo.txt
echo >> systeminfo.txt
echo "Server Disk Space Information" >> systeminfo.txt
echo >> systeminfo.txt
df -h >> systeminfo.txt
echo >> systeminfo.txt
echo "Network Information" >> systeminfo.txt
echo >> systeminfo.txt
route >> systeminfo.txt
echo >> systeminfo.txt
echo "Memory Usage Information" >> systeminfo.txt
echo >> systeminfo.txt
free -g >> systeminfo.txt
echo >> systeminfo.txt
mail -s "System Status Report" manny@aevtech.com < systeminfo.txt
echo "End of Script"

It DOES run at 12:00AM BUT it runs every minute so I get a e-mail EVERY MINUTE. I thought it was only was supposed to run once at 12:00AM and that's it? Not sure what else I am missing for it to only run once every day at night.

Thank you for any and all help!

Change:
Code:

* 0 * *        * /root/scripts/systeminfo.sh
with
Code:

0 0 * *        * /root/scripts/systeminfo.sh

aevtech 12-03-2013 12:52 PM

Hello,

I have made the changes and will await tonight and see but I'm sure it will work. Thank you for your time and help very much!!

gacanepa 12-03-2013 12:58 PM

No problem.
But please note that you don't have to wait until tonight to see if it works :). Change the time the script is supposed to run to a nearer time.
Just FYI, you first crontab entry was sending you an email every minute because that's what the * sign does.
If you type
Code:

* * * * * /path/to/your/script
Then your script will run every minute of every day of every month of every year.
On the other hand,
Code:

45 12 * * * /path/to/your/script
will run your script at exactly 12:45 every day.
You may want to take a look at this link for further details on using cron.
Have a great day!


All times are GMT -5. The time now is 08:56 AM.