Linux crontab execute Python script

This article is originally created by Nascim, who focus on tourism booking technology consulting and undertake the OTA website design as well as GDS access. It consists of the following charters:


  • Issue description
  • Root analysis
  • Solution & Tool
  • Result
  • Conclusion

Issue description

‘crontab -e’ under Linux is commonly used in order to schedule a job, here an explanation from 脚本之家 on how to use ‘crontab’ command. Here I want to execute a python script

0 6,12 * * * python /path/to/my/script/mypythonscript.py >> /path/to/log/crontab.logger 2>&1

In my python script, I’ve used os method as below:

source_cmd_str = "mysql -u%s -p%s --default-character-set=utf8 %s < %s"%(db_user,db_passwd,db_schema,file_handler.name)
os.popen(source_cmd_str)

But I found that this line is never executed, but when I launch this python script in cmd like this:

python /path/to/my/script/mypythonscript.py

The result is correct.

Root analysis

In order to find the reason, I redirected the debug information into the log files like below:

0 6,12 * * * python /path/to/my/script/mypythonscript.py >> /path/to/log/crontab.logger 2>&1

And after execution, I found the error massage:

sh: mysql: command not found

Which indicates that crontab can’t find the path of the command ‘mysql’.

Solution & Tool

It’s easy to add the PATH for the ‘mysql’, either export PATH before launch: os.popen(source_cmd_str)

export PATH=”$PATH:/usr/local/mysql/bin/”

Or just add the path of ‘mysql’ command like:

source_cmd_str = "/usr/local/mysql/bin/mysql -u%s -p%s --default-character-set=utf8 %s < %s"%(db_user,db_passwd,db_schema,file_handler.name)
os.popen(source_cmd_str)

Result

Everything works as designed now!

Conclusion

Some reasons for non-execution of ‘crobtab’ jobs.
1‘crond’ service not stated
‘crontab’ is not a core function under Linux but relays on ‘crond’, so you may need to start ‘crond’ in order to use it.

crond

or

service crond start

If ‘crond’ command can’t be found, you may need to re-install this command under CentOS:

yum -y install crontabs

2 Privilege issue
The script is not executable. You can ‘chmod’ to add execution privilege for it or execute it with ‘bash’ if you’re not sure about it.

bash abc.sh

3 PATH issue
Some command can be executed under shell command line but failure with ‘crontab’. It’s sometimes caused by the unrecognition of the command as a result of wrong PATH configuration. You may insert the PATH into the first line of your script.

#! /usr/local/bin/python

It’s also preferablely use the absolute path of the script in your ‘crontab’:

/usr/loca/bin/python /path/to/my/script/mypythonscript.py 

4 TimeZone
Be aware of the ‘crontab’ use GMT time.

5 Variable
Some variables may not properly assigned value before you recognize that, always try to debug your script before put it into production.


你可能感兴趣的:(Python)