How to run a python file using cron jobs

后端 未结 2 1309
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 00:26

Hi I have created a python file for example as file_example.py

The file will output the sensex value

Suppose the path of the file on linux syste

相关标签:
2条回答
  • 2020-12-01 01:22

    Assuming you are using a unix OS, you would do the following.

    edit the crontab file using the command

    crontab -e
    

    add a line that resembles the one below

    */2 * * * * /Desktop/downloads/file_example.py
    

    this can be used to run other scripts simply use the path to the script needed i.e.

    */2 * * * * /path/to/script/to/run.sh
    

    An explanation of the timing is below (add a star and slash before number to run every n timesteps, in this case every 2 minutes)

    * * * * * command to be executed
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)
    
    0 讨论(0)
  • 2020-12-01 01:22

    You can use python-crontab module.

    https://pypi.python.org/pypi/python-crontab

    To create a new cron job is as simple as follows:

    from crontab import CronTab
    #init cron
    cron   = CronTab()
    
    #add new cron job
    job  = cron.new(command='/usr/bin/echo')
    
    #job settings
    job.hour.every(4)
    
    0 讨论(0)
提交回复
热议问题