bash 将简单的 shell 脚本作为 cronjob 运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19754784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a simple shell script as a cronjob
提问by tomtomssi
I have a very simple shell script I need to run as a cronjob but I can't get even the test scripts to run. Here's and example script:
我有一个非常简单的 shell 脚本,我需要作为 cronjob 运行,但我什至无法运行测试脚本。这是示例脚本:
/home/myUser/scripts/test.sh
/home/myUser/scripts/test.sh
#!/bin/bash
touch file.txt
crontab:
定时任务:
* * * * * /home/myUser/scripts/test.sh
The script runs fine from the terminal but can't get it to run as a cronjob. So far I've tried these in crontab:
该脚本在终端上运行良好,但无法将其作为 cronjob 运行。到目前为止,我已经在 crontab 中尝试过这些:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /bin/bash /home/myUser/scripts/test.sh
And this in the the script file:
这在脚本文件中:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/myUser/scripts
From what I've gathered the solution might be in the PATH variable but I can't figure out what it is since my understanding is very limited at this point. So my question is, how do I get my scripts to run as cronjobs?
从我收集到的解决方案可能在 PATH 变量中,但我无法弄清楚它是什么,因为此时我的理解非常有限。所以我的问题是,如何让我的脚本作为 cronjobs 运行?
EDIT:the file has rwx permissions for all users. This is just for testing purposes.
编辑:该文件对所有用户都有 rwx 权限。这仅用于测试目的。
EDIT:cronjobs such as * * * * * touch /home/myUser/scripts/test.txt
work but it wont run scripts.
编辑:cronjobs 之类的* * * * * touch /home/myUser/scripts/test.txt
工作,但它不会运行脚本。
采纳答案by Gordon Davisson
What directory is file.txt in? cron runs jobs in your home directory, so unless your script cd
s somewhere else, that's where it's going to look for/create file.txt.
file.txt 在哪个目录?cron 在您的主目录中运行作业,因此除非您的脚本cd
位于其他位置,否则它将在那里查找/创建 file.txt。
EDIT: When you refer to a file without specifying its full path (e.g. file.txt
, as opposed to the full path /home/myUser/scripts/file.txt
) in shell, it's taken that you're referring to a file in your current working directory. When you run a script (whether interactively or via crontab), the script's working directory has nothing at all to do with the location of the script itself; instead, it's inherited from whatever ran the script.
编辑:当您在 shell 中引用一个文件而不指定其完整路径(例如file.txt
,与完整路径相反/home/myUser/scripts/file.txt
)时,就认为您指的是当前工作目录中的文件。当你运行一个脚本时(无论是交互的还是通过 crontab 的),脚本的工作目录与脚本本身的位置没有任何关系;相反,它是从运行脚本的任何地方继承的。
Thus, if you cd
(change working directory) to the directory the script's in and then run it, file.txt
will refer to a file in the same directory as the script. But if you don't cd
there first, file.txt
will refer to a file in whatever directory you happen to be in when you ran the script. For instance, if your home directory is /home/myUser, and you open a new shell and immediately run the script (as scripts/test.sh
or /home/myUser/scripts/test.sh
; ./test.sh
won't work), it'll touch the file /home/myUser/file.txt because /home/myUser is your current working directory (and therefore the script's).
因此,如果您cd
(更改工作目录)到脚本所在的目录然后运行它,file.txt
将引用与脚本位于同一目录中的文件。但是,如果您不cd
先file.txt
在那里,将引用您运行脚本时碰巧所在的任何目录中的文件。例如,如果您的主目录是 /home/myUser,并且您打开一个新的 shell 并立即运行脚本(asscripts/test.sh
或/home/myUser/scripts/test.sh
;./test.sh
不起作用),它会触及文件 /home/myUser/file.txt 因为 / home/myUser 是您当前的工作目录(也是脚本的目录)。
When you run a script from cron
, it does essentially the same thing: it runs it with the working directory set to your home directory. Thus all file references in the script are taken relative to your home directory, unless the script cd
s somewhere else or specifies an absolute path to the file.
当您从 运行脚本时cron
,它做的事情本质上是一样的:它运行时将工作目录设置为您的主目录。因此,脚本中的所有文件引用都是相对于您的主目录进行的,除非脚本cd
位于其他地方或指定了文件的绝对路径。
回答by tomtomssi
The easiest way would be to use a GUI:
最简单的方法是使用 GUI:
For Gnome use gnome-schedule (universe)
对于 Gnome 使用gnome-schedule (universe)
sudo apt-get install gnome-schedule
For KDE use kde-config-cron
对于 KDE 使用kde-config-cron
It should be pre installed on Kubuntu
But if you use a headless linux or don′t want GUI′s you may use:
但是,如果您使用无头 linux 或不想要 GUI,您可以使用:
crontab -e
If you type it into Terminal you′ll get a table.
You have to insert your cronjobs now.
Format a job like this:
如果你在终端中输入它,你会得到一张桌子。
您现在必须插入您的 cronjobs。
像这样格式化作业:
* * * * * YOURCOMMAND
- - - - -
| | | | |
| | | | +----- Day in Week (0 to 7) (Sunday is 0 and 7)
| | | +------- Month (1 to 12)
| | +--------- Day in Month (1 to 31)
| +----------- Hour (0 to 23)
+------------- Minute (0 to 59)
There are some shorts, too (if you don′t want the *):
还有一些短裤(如果你不想要 *):
@reboot --> only once at startup
@daily ---> once a day
@midnight --> once a day at midnight
@hourly --> once a hour
@weekly --> once a week
@monthly --> once a month
@annually --> once a year
@yearly --> once a year
If you want to use the shorts as cron (because they don′t work or so):
如果您想将短裤用作 cron(因为它们不起作用):
@daily --> 0 0 * * *
@midnight --> 0 0 * * *
@hourly --> 0 * * * *
@weekly --> 0 0 * * 0
@monthly --> 0 0 1 * *
@annually --> 0 0 1 1 *
@yearly --> 0 0 1 1 *
回答by Ranjithkumar T
Try,
尝试,
# cat test.sh
#!/bin/bash
/bin/touch file.txt
cron as:
cron 为:
* * * * * /bin/sh /home/myUser/scripts/test.sh
And you can confirm this by:
您可以通过以下方式确认:
# tailf /var/log/cron
回答by Sanidhya Srivastava
Specify complete path and grant proper permission to scriptfile. I tried following script file to run through cron:
指定完整路径并授予脚本文件适当的权限。我尝试使用以下脚本文件来运行 cron:
#!/bin/bash
/bin/mkdir /scratch/ofsaaweb/CHEF_FICHOME/ficdb/bin/crondir
And crontab command is
而 crontab 命令是
* * * * * /bin/bash /scratch/ofsaaweb/CHEF_FICHOME/ficdb/bin/test.sh
It worked for me.
它对我有用。
回答by virus
It should run properly at cron also. Please check below things.
它也应该在 cron 上正常运行。请检查以下事项。
1- You are editing proper file to set cron.
1- 您正在编辑正确的文件以设置 cron。
2- You have given proper permission(execute permission) to script mean your script is executable.
2-您已授予脚本适当的权限(执行权限)意味着您的脚本是可执行的。