Mike
Mike

Reputation: 415

Where to locate Centos 6 cron job .sh file

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:

  1. 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.
  2. 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.

Upvotes: 0

Views: 1708

Answers (1)

sasoiliev
sasoiliev

Reputation: 81

I believe that cron will execute the script from whichever directory it is in, given that:

  • the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
  • the cron line specifies the full path to the script

So, if your script is /opt/script.sh, specifying this in cron:

0 0 * * * /opt/script.sh

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:

0 0 * * * username /opt/script.sh

Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). 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.

Upvotes: 1

Related Questions