I want to monitoring cron jobs using shell script.
But if I use variable contain exit code of last command I have "0" always.
I think it's because I run this script after cron job and "exit code" takes value "0" (start script)
How can I ignore start of the script and use "exit code" variable of previous cron job?

2 Answers
2

The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.

The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.

I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.

You need to fix the cron job to save its exit status to some status file; then you can read that status file later.