我通常有几个问题,如何cron执行脚本,因为他们通常没有我的环境设置。是否有一种方法可以调用bash(?)以同样的方式cron,这样我就可以在安装它们之前测试脚本?
当前回答
Cron默认只提供这个环境:
HOME用户的主目录 LOGNAME用户登录名 路径= / usr / bin: / usr / sbin SHELL = / usr / bin / sh
如果需要更多,可以在crontab中的调度表之前定义环境。
其他回答
默认情况下,cron使用您系统中的sh来执行它的作业。这可能是实际的Bourne shell或破折号,ash, ksh或bash(或另一个)符号链接到sh(并因此在POSIX模式下运行)。
最好的方法是确保您的脚本拥有它们需要的东西,并假设没有为它们提供任何东西。因此,您应该使用完整的目录规范并自己设置环境变量(如$PATH)。
六年后回答:环境不匹配问题是systemd“计时器”作为cron替代品解决的问题之一。无论您是从CLI还是通过cron运行systemd“service”,它都接收到完全相同的环境,从而避免了环境不匹配的问题。
导致cron任务在手动传递时失败的最常见问题是cron设置的限制性默认$PATH,在Ubuntu 16.04中是这样的:
"/usr/bin:/bin"
相比之下,systemd在Ubuntu 16.04上设置的默认$PATH是:
"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
因此,systemd计时器已经有更好的机会找到二进制文件,而不需要进一步的麻烦。
systemd计时器的缺点是,设置它们需要更多的时间。您首先创建一个“服务”文件来定义您想要运行的内容,然后创建一个“计时器”文件来定义运行它的时间表,最后“启用”计时器来激活它。
你可以运行:
env - your_command arguments
这将在空环境下运行your_command。
别忘了,因为cron的父进程是init,所以它运行的程序没有控制终端。你可以用这样的工具来模拟:
http://libslack.org/daemon/
一些方法:
Export cron env and source it: Add * * * * * env > ~/cronenv to your crontab, let it run once, turn it back off, then run env - `cat ~/cronenv` /bin/sh And you are now inside a sh session which has cron's environment Bring your environment to cron You could skip above exercise and just do a . ~/.profile in front of your cron job, e.g. * * * * * . ~/.profile; your_command Use screen Above two solutions still fail in that they provide an environment connected to a running X session, with access to dbus etc. For example, on Ubuntu, nmcli (Network Manager) will work in above two approaches, but still fail in cron. * * * * * /usr/bin/screen -dm Add above line to cron, let it run once, turn it back off. Connect to your screen session (screen -r). If you are checking the screen session has been created (with ps) be aware that they are sometimes in capitals (e.g. ps | grep SCREEN) Now even nmcli and similar will fail.