bjohnson
bjohnson

Reputation: 96

Shell script hanging when calling cat

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" [email protected];
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?

Upvotes: 1

Views: 1831

Answers (1)

Jack Bracken
Jack Bracken

Reputation: 1283

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).

Upvotes: 4

Related Questions