我想为cron写一个单行代码:
我有一个脚本,该脚本将打印一些内容,然后严重退出。
script.sh:
SCRIPT=`basename "$0"`
echo $SCRIPT is running...
\>&2 echo $SCRIPT has failed!
exit 1
当我运行脚本时没有管道到记录器,一切都很好。当我以后将所有内容通过管道传送到记录器以供查看时,错误检查将失败。有没有办法使管道和错误检查同时工作?
\>script.sh
script.sh is running...
script.sh failed!
\>script.sh || echo foo
script.sh is running...
script.sh failed!
foo
\>script.sh 2>&1 | logger || echo foo
\>
答案 0 :(得分:0)
一种方法:
SELECT u.userName, u.city
FROM (SELECT DISTINCT userName, city FROM projects) u
LEFT JOIN projects p
ON u.userName = p.userName AND
MONTH(p.date) = MONTH(CURRENT_DATE()) AND
YEAR(p.date) = YEAR(CURRENT_DATE())
WHERE
p.userName IS NULL
and u.city = 'Paris'
另一种方式:
set -o pipefail
script.sh |& logger || echo foo
请参见man bash
:
(script.sh |& logger; exit ${PIPESTATUS[0]}) || echo foo
一个数组变量,包含最近执行的进程的退出状态值列表 前台管道(可能只包含一个命令)。