运行一个简单的shell脚本作为一个cronjob [英] Running a simple shell script as a cronjob
问题描述
我有一个非常简单的shell脚本,我需要作为一个cronjob运行,但我不能让连测试脚本运行。下面是和示例脚本:
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:
/home/myUser/scripts/test.sh
/home/myUser/scripts/test.sh
#!/bin/bash
touch file.txt
的crontab:
crontab:
* * * * * /home/myUser/scripts/test.sh
脚本运行从终端罚款,但不能让它作为一个cronjob运行。到目前为止,我已经在crontab中尝试这些:
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:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /bin/bash /home/myUser/scripts/test.sh
这在脚本文件:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/myUser/scripts
从我收集到的解决方案可能是在PATH变量中,但我无法弄清楚它是什么,因为我的理解是在这一点上是非常有限的。所以我的问题是,我该如何让我的脚本为cronjobs运行?
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?
编辑:文件对所有用户的rwx权限。这只是用于测试目的。
the file has rwx permissions for all users. This is just for testing purposes.
编辑: cronjobs如 * * * * *触摸/home/myUser/scripts/test.txt
的工作,但它不会运行脚本
cronjobs such as * * * * * touch /home/myUser/scripts/test.txt
work but it wont run scripts.
推荐答案
什么目录在FILE.TXT?的cron作业运行在你的主目录,所以,除非你的脚本 CD
取值别的地方,这就是它要查找/创建file.txt的。
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的
,而不是完整的路径 /家庭/ MYUSER /脚本/ file.txt的
)的外壳,它采取您提到一个文件在当前的工作目录。当你运行一个脚本(无论是交互方式或通过crontab中),脚本的工作目录有什么都没有做的脚本本身的位置;相反,它无论从任何运行脚本继承。
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.
因此,如果您 CD
(改变工作目录)的脚本,然后运行它的目录, file.txt的
将引用文件相同的目录中。但是,如果你不这样做 CD
有第一, file.txt的
将引用你碰巧到任何目录中的文件在当你运行脚本。举例来说,如果你的主目录是/ home / MYUSER,你打开一个新的外壳,立即运行脚本(如脚本/ test.sh
或 /home/myUser/scripts/test.sh
; ./ test.sh
将无法正常工作),它会接触到文件/家/ MYUSER / file.txt的,因为的/ home / MYUSER是当前工作目录(以及脚本的)。
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).
当您从的cron
的脚本,但它本质上是一样的东西:它设置为你的home目录的工作目录运行它。因此,在脚本中的所有文件的引用将被相对于你的home目录,除非脚本 CD
取值别的地方,或者指定的文件的绝对路径。
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.
这篇关于运行一个简单的shell脚本作为一个cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!