大网数据广告活动
服务器运维问答

cron not running bash commands

· 52CE 我爱测

I am using the whenever gem to generate cron commands. I can check these commands with crontab -l . The generated commands are 24 15 * * * /bin/bash -l -c 'cd /Users/Chris/Sites/covid_tracker && RAILS_ENV=development bundle exec …

问题原文

原帖链接 · Obromios · CC BY-SA 4.0

I am using the whenever gem to generate cron commands. I can check these commands with crontab -l. The generated commands are

24 15 * * * /bin/bash -l -c 'cd /Users/Chris/Sites/covid_tracker && RAILS_ENV=development bundle exec rake countries:temp --silent >> log/cron.log 2>&1'

24 15 * * * /bin/bash -l -c 'echo '\''should see this in the log'\'' >> log/cron.log 2>&1'

The first command (bundle exec rake) runs and I can see the ouptut in the cron.log. However, I see no output for the second command (echo) in the cron.log.

I want the echo command (and other bash commands I want to use) to run in the same directory as the bundle exec rake command, and the permissions to be same as if I had issue the command from that directory. How do I do this?

已采纳解答

原帖链接 · aleksikallio · CC BY-SA 4.0

You can prefix the command with the same cd /Users/Chris/Sites/covid_tracker && as the first command. This changes the working directory to that directory before running the command. Another option would be to create a shell script in that directory and run that. For example:

24 15 * * * cd /Users/Chris/Sites/covid_tracker && ./runCron.sh >> log/cron.log 2>&1

runCron.sh:

#!/bin/bash -l

cd /Users/Chris/Sites/covid_tracker
RAILS_ENV=development bundle exec rake countries:temp --silent
echo "should see this in the log"

(I'm not sure why you've specified a login shell with the -l param, is that needed for rake?)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。