crontab当中运行命令包含特殊字符的bug
date
Nov 7, 2021
slug
tip-crontab-里面有特殊字符
status
Published
summary
小技巧
tags
tip
linux
cron
type
Post
URL
Problem & Summary
- 发现搞了半天都无法在crontab当中执行成功;而直接运行是OK的
/usr/bin/docker exec -it calibre-web /bin/bash -c "cp /books/metadata.db /books/metadata_$(date +'%Y_%m_%d_%H_%M').db && calibredb add --library-path=/books -r /books/_new2add/; rm -rf /books/_new2add/*" >> /var/log/calibre-web-addbook.log 2>&1
- 查看了日子 journalctl -f -u cron

- 看起来是当中的 && 符号被cron给切掉了
Soltion
把需要运行的命令放到script当中
cat > /root/calibre-web-refresh.sh <<EOF
#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/bin/docker exec calibre-web /bin/bash -c "cp /books/metadata.db /books/metadata_$(date +'%Y_%m_%d_%H_%M').db && calibredb add --library-path=/books -r /books/_new2add/; rm -rf /books/_new2add/*"
EOF
chmod +x /root/calibre-web-refresh.sh
再把这句加到 crontab -e 当中
*/15 * * * * /root/calibre-web-refresh.sh >> /var/log/calibre-web-addbook.log 2>&1

Screenshots

