I am really new to Linux and I apologize if this is rudimentary, but I have Google’d to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
- I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
- Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don’t screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that
cronwill execute the script from whichever directory it is in, given that:cronruns as (usuallyrootif job is configured in the system-wide crontab)cronline specifies the full path to the scriptSo, if your script is /opt/script.sh, specifying this in cron:
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
Also, something to make sure when working with
cronis to either use full paths when calling external commands from the script or to set up thePATHvariable (either in the script itself or on thecrontabfile). This is needed because usually the environment in which cron jobs are run is pretty restricted.Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it’s output to a log file or syslog.