简体   繁体   English

如何在linux上的cron作业中安排bash文件

[英]How to schedule a bash file in cron jobs on linux

i am trying to schedule a bash script (cron.sh) in cron jobs below is my bash script code 我试图在下面的cron作业中安排一个bash脚本(cron.sh)是我的bash脚本代码

cron.sh : (Please correct it if something wrong) cron.sh :(如果出错了请纠正)

#!/bin/bash 
#chmod +x cron.sh

cd ~/main_file/folders/douys
python -u cron.py 

Please let me know how to make a bash file executable 请告诉我如何使bash文件可执行

Here cron.sh file runs some python file, and when i run the bash script (cron.sh) with the following command it works fine and python file is executing 这里cron.sh文件运行一些python文件,当我使用以下命令运行bash脚本(cron.sh)时它工作正常并且python文件正在执行

sh cron.sh

But i want to schedule the above cron.sh file in crons jobs to run for every two mins 但我想安排crons作业中的上述cron.sh文件每两分钟运行一次

can anyone let me know how to do above 任何人都可以让我知道如何做到上面

It's worth mentioning that, if your .sh is just a "wrapper" for the .py, you can add the following line to the beginning of you Python file: 值得一提的是,如果你的.sh只是.py的“包装器”,你可以在Python文件的开头添加以下行:

#!/usr/bin/python

And then make it executable: 然后使其可执行:

$> chmod a+x code.py

Then you can add directly your Python file as a cronjob following the instruction that m4573r explained. 然后,您可以按照m4573r解释的说明直接将您的Python文件添加为cronjob。 (of course, '~/cron.sh' becomes 'path/to/cron.py'). (当然,'〜/ cron.sh'变成'path / to / cron.py')。

You can schedule the job by running crontab -e and adding the following line in the end: */2 * * * * /path/to/cron.sh . 您可以通过运行crontab -e并在最后添加以下行来安排作业: */2 * * * * /path/to/cron.sh

You should read man cron for more informations. 你应该阅读man cron获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM