调用 cat 时 shell 脚本挂起 [英] Shell script hanging when calling cat

查看:49
本文介绍了调用 cat 时 shell 脚本挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 shell 脚本,它通过电子邮件向我发送通过 crontasks 遇到的错误,如下所示:

exec >&-;输出=$(猫)";shopt -s nocasematchif [[ "$output" == *"error"* ||"$output" == *"warning"* ]];然后echo "$output" |mail -s "错误" my@email.com;菲退出 0;

我的 crontab 看起来像:

*/1 * * * */opt/sh/email.sh/usr/bin/php/home/sites/website/app/console my:cli:command >>/var/log/cron.d/my.cli.command/log 2>&1

脚本有效,但cat"似乎挂了:

root 23083 0.0 0.0 139752 1112 ?S Mar20 0:00 \_ CROND500 23091 0.0 0.0 106096 1016 ?Ss Mar20 0:00 |\_/bin/sh -c/usr/bin/php/var/www/website/app/console my:cli:command 2>&1 |/usr/local/bin/email.sh500 23096 0.0 0.3 463528 27292 ?S Mar20 0:35 |\_/usr/bin/php/var/www/website/app/console my:cli:command500 23097 0.0 0.0 106096 1048 ?3 月 20 日 0:00 |\_/bin/bash/usr/local/bin/email.sh500 23101 0.0 0.0 100936 496 ?3 月 20 日 0:00 |\_ 猫根 12167 0.0 0.0 139752 1276 ?S Mar22 0:00 \_ CROND500 12183 0.0 0.0 106096 1104 ?Ss Mar22 0:00 |\_/bin/sh -c/usr/bin/php/var/www/website/app/console my:cli:command 2>&1 |/usr/local/bin/email.sh500 12185 0.0 0.4 463528 36612 ?S Mar22 0:32 |\_/usr/bin/php/var/www/website/app/console my:cli:command500 12186 0.0 0.0 106096 1104 ?3 月 22 日 0:00 |\_/bin/bash/usr/local/bin/email.sh500 12194 0.0 0.0 100936 516 ?3 月 22 日 0:00 |\_ 猫根 1675 0.0 0.0 139752 1128 ?S Mar25 0:00 \_ CROND

有什么想法吗?

解决方案

它挂起是因为你没有给 cat 任何连接输入,所以它只会永远听 STDIN.

来自手册页:

<块引用>

cat 实用程序按顺序读取文件,将它们写入标准输出.文件操作数按命令行顺序处理.如果 file 是单个破折号 (`-') 或不存在,则 cat 从标准输入中读取.如果文件是 UNIX 域套接字,则 cat 连接到它然后读取它直到 EOF.这补充了 inetd(8) 中可用的 UNIX 域绑定功能.

I have a shell script which emails me errors encountered via crontasks which looks like the following:

exec >&-;

output="$(cat)";

shopt -s nocasematch

if [[ "$output" == *"error"* || "$output" == *"warning"* ]]; then
    echo "$output" | mail -s "Error" my@email.com;
fi

exit 0;

My crontab looks like:

*/1 * * * * /opt/sh/email.sh /usr/bin/php /home/sites/website/app/console my:cli:command >> /var/log/cron.d/ my.cli.command/log 2>&1

The script works, but "cat" seem to hang:

root 23083 0.0 0.0 139752 1112 ? S Mar20 0:00 \_ CROND
500 23091 0.0 0.0 106096 1016 ? Ss Mar20 0:00 | \_ /bin/sh -c /usr/bin/php /var/www/website/app/console my:cli:command 2>&1 | /usr/local/bin/email.sh
500 23096 0.0 0.3 463528 27292 ? S Mar20 0:35 | \_ /usr/bin/php /var/www/website/app/console my:cli:command
500 23097 0.0 0.0 106096 1048 ? S Mar20 0:00 | \_ /bin/bash /usr/local/bin/email.sh
500 23101 0.0 0.0 100936 496 ? S Mar20 0:00 | \_ cat
root 12167 0.0 0.0 139752 1276 ? S Mar22 0:00 \_ CROND
500 12183 0.0 0.0 106096 1104 ? Ss Mar22 0:00 | \_ /bin/sh -c /usr/bin/php /var/www/website/app/console my:cli:command 2>&1 | /usr/local/bin/email.sh
500 12185 0.0 0.4 463528 36612 ? S Mar22 0:32 | \_ /usr/bin/php /var/www/website/app/console my:cli:command
500 12186 0.0 0.0 106096 1104 ? S Mar22 0:00 | \_ /bin/bash /usr/local/bin/email.sh
500 12194 0.0 0.0 100936 516 ? S Mar22 0:00 | \_ cat
root 1675 0.0 0.0 139752 1128 ? S Mar25 0:00 \_ CROND

Any ideas out there?

解决方案

It is hanging because you're not giving cat any input to concatenate, so it will just listen to STDIN forever.

From the man page:

The cat utility reads files sequentially, writing them to the standard output. The file operands are processed in command-line order. If file is a single dash (`-') or absent, cat reads from the standard input. If file is a UNIX domain socket, cat connects to it and then reads it until EOF. This complements the UNIX domain binding capability available in inetd(8).

这篇关于调用 cat 时 shell 脚本挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆