Run a script in Python with Crontab

1

I need to execute a script that is programmed in Python so that it runs every so often, specifically every 3 minutes. I am using crontab and the file that I modified was the following:

crontab -e

Within that file the content that you place is the following:

    #M      H       D       DS      M
    */3     *       *       *       *       /usr/local/nagios/nan/mailer/mailer.py >> /usr/local/nagios/nan/mailer/mailer.log
    0       0       *       *       *       /usr/local/nagios/bin/sched_downtime

If I run the file manually separately it runs correctly. How can I run it every 3 minutes?

The cron log located in the following path / var / log / cron is as follows:

Jun 21 16:23:35 ip-172-31-8-52 crond [2641]: (CRON) STARTUP (1.4.4) Jun 21 16:23:35 ip-172-31-8-52 crond [2641]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 17% if used.) Jun 21 16:23:35 ip-172-31-8-52 crond [2641]: (CRON) INFO (running with inotify support) Jun 21 16:24:02 ip-172-31-8-52 CROND [2794]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:27:01 ip-172-31-8-52 CROND [3222]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:30:01 ip-172-31-8-52 CROND [3605]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:33:01 ip-172-31-8-52 CROND [3982]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:36:01 ip-172-31-8-52 CROND [4362]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:39:01 ip-172-31-8-52 CROND [4739]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> & gs; / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:42:01 ip-172-31-8-52 CROND [5119]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:45:01 ip-172-31-8-52 CROND [5495]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> & gs; / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:48:01 ip-172-31-8-52 CROND [5911]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> & gs; / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:51:01 ip-172-31-8-52 CROND [6285]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:54:01 ip-172-31-8-52 CROND [6670]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 16:57:01 ip-172-31-8-52 CROND [7044]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> & gs; / usr / local / nagios /nan/mailer/mailer.log) Jun 21 17:00:01 ip-172-31-8-52 CROND [7425]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 17:01:01 ip-172-31-8-52 CROND [7515]: (root) CMD (run-parts /etc/cron.hourly) Jun 21 17:01:01 ip-172-31-8-52 run-parts (/etc/cron.hourly) [7515]: starting 0anacron Jun 21 17:01:01 ip-172-31-8-52 anacron [7525]: Anacron started on 2018-06-21 Jun 21 17:01:01 ip-172-31-8-52 run-parts (/etc/cron.hourly) [7527]: finished 0anacron Jun 21 17:01:01 ip-172-31-8-52 anacron [7525]: Will run job cron.daily' in 14 min. Jun 21 17:01:01 ip-172-31-8-52 anacron[7525]: Jobs will be executed sequentially Jun 21 17:03:01 ip-172-31-8-52 CROND[7815]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py >> /usr/local/nagios/nan/mailer/mailer.log) Jun 21 17:06:01 ip-172-31-8-52 CROND[8198]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py >> /usr/local/nagios/nan/mailer/mailer.log) Jun 21 17:09:01 ip-172-31-8-52 CROND[8574]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py >> /usr/local/nagios/nan/mailer/mailer.log) Jun 21 17:12:01 ip-172-31-8-52 CROND[8960]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py >> /usr/local/nagios/nan/mailer/mailer.log) Jun 21 17:15:01 ip-172-31-8-52 anacron[7525]: Job cron.daily 'started Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9368]: starting logrotate Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9376]: finished logrotate Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9368]: starting man-db.cron Jun 21 17:15:01 ip-172-31-8-52 CROND [9386]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9396]: finished man-db.cron Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9368]: starting tmpwatch Jun 21 17:15:01 ip-172-31-8-52 run-parts (/etc/cron.daily) [9422]: finished tmpwatch Jun 21 17:15:01 ip-172-31-8-52 anacron [7525]: Job 'cron.daily' terminated Jun 21 17:15:01 ip-172-31-8-52 anacron [7525]: Normal exit (1 job run) Jun 21 17:18:01 ip-172-31-8-52 CROND [9798]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 17:21:01 ip-172-31-8-52 CROND [10229]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log) Jun 21 17:24:01 ip-172-31-8-52 CROND [10817]: (nagios) CMD (/usr/local/nagios/nan/mailer/mailer.py> > / usr / local / nagios /nan/mailer/mailer.log)

The output of the log is as follows:

    
asked by Guillermo Ricardo Spindola Bri 20.06.2018 в 18:46
source

1 answer

0

Maybe if your script ran but since it is not as executable or had a shebang , then it did nothing.

In that case, and if you want to try it, you should put at the beginning of your file mailer.py something like.

#!/usr/bin/env python
...
todo el código python que hiciste
...

And you give him execution permissions with chmod +x mailer.py . And your crontab should already run with the simple path of the mailer.py file, as you showed it in your question.

To see the logs of a scheduled task with crontab you can review the file /var/log/syslog like this.

$ sudo tail -f /var/log/syslog | grep "CRON"

Then you can see them in "real" time.

In case you want to create an error file, when creating a crontab you should redirect the output of what is executed by the script, for example.

*/3 * * * * programa 2>> /ruta/a/error.log

or

*/3 * * * * programa >> /ruta/a/all.log 2>&1

With this last both stdout and stdin go to the error file.

Another option is, as you suggest in the comments, that you have to put what program that file will run, thinking that you did not put a shebang . It worked for me simply by putting something like python /ruta/completa/del/archivo.py or with cd /carpeta/del/archivo && python archivo.py , that is, I have not had problems with the location of python since in the documentation of man crontab(5) the following is established.

  

Several environment variables are set up automatically by the cron (8) daemon. SHELL is set to /bin/sh , and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. PATH is set to /usr/bin:/bin . HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is running from, and may not be changed.

And since I have python located in /usr/bin/python , I have no problem. To locate it do not use which , use command -v python since which only checks if it is in your PATH but command returns the way in which bash interprets that command, and, within a script, shows the full path of that program.

In case you are in paranoid mode (I know that sometimes you get superstitious) you can run the crontab with.

*/3 * * * * /donde/esta/tu/python /archivo/a/correr.py

Another option you can use is to create a bash script that is a wrapper.

For example.

#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin:~/bin  #O el PATH que quieras para ese wrapper.
_LOG_FILE=/ruta/a/registro.log
_LOG_ERROR_FILE=/ruta/a/error.log

#Puedes añadir más cosas a tu wrapper para turbo personalizar tus logs
#o forma de llamar a tus programas.

python /la/ruta/a/tu/programa.py >> $_LOG_FILE 2>> $_LOG_ERROR_FILE
# o puedes hacer algo como
python /ruta/programa.py > >(tee -a $_LOG_FILE) 2> >(tee -a $_LOG_ERROR_FILE >&2)

When you finish editing this wrapper you need to give execution permissions with chmod +x tu_wrapper

And now you can run your scheduled task with the simple wrapper path and in a personalized way. Now you can run, you already have the shebang so you know you have to run with /bin/bash , have the PATH you want and have a record handling.

*/3 * * * * /ruta/a/tu/wrapper
    
answered by 28.11.2018 в 05:17