简体   繁体   English

管道无法在crontab中运行的Bash命令

[英]Bash command with pipe not working in crontab

I need to delete files under directory except latest 2. 我需要删除目录,除了最新的2。

I have prepared command to list according to date and delete files. 我准备了根据日期列出的命令并删除文件。 It work when I run manually from command line , however it does not work in crontab. 当我从命令行手动运行时,它工作,但它在crontab中不起作用。

In crontab, 在crontab中,

* * * * * /bin/ls -t /mytest | /usr/bin/tail -n +2 | /usr/bin/xargs rm --

This command works when I run this command from commandline. 从命令行运行此命令时,此命令有效。

Also tried to add command in bash script then called that script from crontab but it did not work again. 还尝试在bash脚本中添加命令,然后从crontab调用该脚本,但它不再起作用。

How can I run that command via crontab? 如何通过crontab运行该命令?

crontab only expects one command. crontab只需要一个命令。 If multiple commands are to be executed, they can be bundled using bash -c 如果要执行多个命令,可以使用bash -c捆绑它们

* * * * * bash -c 'ls -t /mytest | tail -n +2 | xargs rm --'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM