bash脚本,
#! /bin/bash
echo 'hi bash'
aa
python ~/fledgling/cron/test.py
echo 'after python script'
这里 aa
是别名,它激活了virtualenv
alias aa='cd /home/fledgling/projects/myblog/base'
python脚本,
import time
with open('~/fledgling/cron/%s.txt' % time.strftime("%Y_%m_%d__%H:%M:%S"), 'w') as f:
f.write('%s' % time.strftime("%Y_%m_%d__%H:%M:%S"))
我想每隔一分钟运行一次这个python脚本 .
crontab中,
* * * * * ~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt
〜/ fledgling / cron / log.txt的内容,
hi bash
after python script
输出/ var / log / syslog
✘ ✝ ~/fledgling/cron tail -f /var/log/syslog
Jun 24 13:46:01 mysys CRON[14697]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt)
Jun 24 13:46:01 mysys CRON[14696]: (CRON) info (No MTA installed, discarding output)
Jun 24 13:47:01 mysys CRON[14736]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt)
Jun 24 13:47:01 mysys CRON[14735]: (CRON) info (No MTA installed, discarding output)
Jun 24 13:48:01 mysys CRON[14747]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt)
Jun 24 13:48:01 mysys CRON[14746]: (CRON) info (No MTA installed, discarding output)
Jun 24 13:48:11 mysys crontab[14769]: (mysys) BEGIN EDIT (mysys)
Jun 24 13:48:59 mysys crontab[14769]: (mysys) END EDIT (mysys)
Jun 24 13:49:01 mysys CRON[14794]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt)
Jun 24 13:49:01 mysys CRON[14793]: (CRON) info (No MTA installed, discarding output)
权限,
-rw-rw-r-- 1 mysys mysys 476 Jun 24 13:57 log.txt
-rwxrw-r-- 1 mysys mysys 153 Jun 24 13:37 test.py
-rwxrwxr-x 1 mysys mysys 91 Jun 24 13:32 test.sh
我想要的是 ?我想每分钟创建一个文本文件 .
我的问题是什么?我可以在日志中看到每分钟bash脚本运行后没有错误,那么为什么python脚本没有被执行或者没有创建任何文本文件?
4 回答
在shell脚本中使用完整路径而不是别名 .
AA = /家庭/初出茅庐/项目/ myblog /基
cd $ aa
问题是你不能在python中使用
~
,在shell中不能使用which will be expanded而不是python .由于一些答案提到,有一点要宣告的是
我在wikipedia看到了这个,我在Mac上检查过,这是真的 .
没有〜在Python中
之后没有空格!在bash Headers 中
不确定cron发起的bash会猜你的别名
不确定crontab接受〜
查看您的日志(/ var / log / syslog)
在crontab中尝试重定向2> /tmp/debug.log
检查test.sh是否可执行
如果您在服务器上运行该脚本,那么您可能没有使用: