将简单的 shell 脚本作为 cronjob 运行 [英] Running a simple shell script as a cronjob

查看:28
本文介绍了将简单的 shell 脚本作为 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

定时任务:

* * * * * /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 如 * * * * * touch/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 cds somewhere else, that's where it's going to look for/create file.txt.

当您引用一个文件而不指定其完整路径时(例如 file.txt,而不是完整路径 /home/myUser/scripts/file.txt) 在 shell 中,则认为您指的是当前工作目录中的文件.当你运行一个脚本时(无论是交互的还是通过 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 将引用同一目录中的文件作为脚本.但是,如果您没有先cdfile.txt 将引用您运行脚本时碰巧所在的任何目录中的文件.例如,如果您的主目录是/home/myUser,并且您打开一个新的 shell 并立即运行脚本(如 scripts/test.sh/home/myUser/scripts/test.sh; ./test.sh 不起作用),它会触及文件/home/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 运行脚本时,它做的事情本质上是一样的:它运行时将工作目录设置为您的主目录.因此,脚本中的所有文件引用都是相对于您的主目录进行的,除非脚本 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 cds somewhere else or specifies an absolute path to the file.

这篇关于将简单的 shell 脚本作为 cronjob 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆