main方法启动job的shell脚本,如何通过Cron Job运行Shell脚本

I have a Shell script like below

echo "Hello World"

The script is located in /root/scripts/ folder as test.sh

I also created a cron job like below

0-59 * * * * ./scripts/test.sh

Now the cron job is not printing the content in test.sh every minute.

Let me know whether I have given a wrong directory or I have any other problem in my code.

解决方案

I would

Be explicit wrt. your directory to execute from e.g. /root/scripts/test.sh. I don't know what cron would regard as the current directory

Redirect stdout to a log file e.g. ...test.sh > /tmp/cron.log (you would likely want to redirect stderr at some stage too using 2>&1). Otherwise you're not going to see the output. It gets mailed to the cronjob owner

Make sure you've given execution permission to your bash script (chmod +x /root/scripts/test.sh)

Be explicit which script executable will execute your shell script. It's good practise to have a script invocation at the top e.g. #!/bin/sh or similar

Getting stuff to run under cron is notoriously tricky. Cron jobs run with a massively cut-down environment. It's instructive to print out the environment available to your script (use env) and compare/contrast with what you have available from your interactive shell.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值