Cron jobs monitoring using exit code
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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?
$ ls foo ; echo $?
ls: cannot access foo: No such file or directory
2
$ ls foo
ls: cannot access foo: No such file or directory
$ sh test.sh
0
My test.sh:
#!/bin/bash
notify()
EXIT_CODE=$?
echo $EXIT_CODE
notify
linux shell-script shell cron exit-code
add a comment |Â
up vote
0
down vote
favorite
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?
$ ls foo ; echo $?
ls: cannot access foo: No such file or directory
2
$ ls foo
ls: cannot access foo: No such file or directory
$ sh test.sh
0
My test.sh:
#!/bin/bash
notify()
EXIT_CODE=$?
echo $EXIT_CODE
notify
linux shell-script shell cron exit-code
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
$ ls foo ; echo $?
ls: cannot access foo: No such file or directory
2
$ ls foo
ls: cannot access foo: No such file or directory
$ sh test.sh
0
My test.sh:
#!/bin/bash
notify()
EXIT_CODE=$?
echo $EXIT_CODE
notify
linux shell-script shell cron exit-code
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?
$ ls foo ; echo $?
ls: cannot access foo: No such file or directory
2
$ ls foo
ls: cannot access foo: No such file or directory
$ sh test.sh
0
My test.sh:
#!/bin/bash
notify()
EXIT_CODE=$?
echo $EXIT_CODE
notify
linux shell-script shell cron exit-code
asked Jun 6 at 11:49
Pasily_V
1
1
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
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.
add a comment |Â
up vote
0
down vote
You can pass the exit code as a parameter to your script:
ls -l /tmp/filethatdoesnotex.ist
/usr/local/bin/test.sh $?
with test.sh:
RESULT=$1
if [ $RESULT -gt 0 ]
then
echo "something bad happened and ended with result $RESULT" >&2
else
echo "everything is fine, sleep on"
fi
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Jun 6 at 11:57
wurtel
8,7151822
8,7151822
add a comment |Â
add a comment |Â
up vote
0
down vote
You can pass the exit code as a parameter to your script:
ls -l /tmp/filethatdoesnotex.ist
/usr/local/bin/test.sh $?
with test.sh:
RESULT=$1
if [ $RESULT -gt 0 ]
then
echo "something bad happened and ended with result $RESULT" >&2
else
echo "everything is fine, sleep on"
fi
add a comment |Â
up vote
0
down vote
You can pass the exit code as a parameter to your script:
ls -l /tmp/filethatdoesnotex.ist
/usr/local/bin/test.sh $?
with test.sh:
RESULT=$1
if [ $RESULT -gt 0 ]
then
echo "something bad happened and ended with result $RESULT" >&2
else
echo "everything is fine, sleep on"
fi
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can pass the exit code as a parameter to your script:
ls -l /tmp/filethatdoesnotex.ist
/usr/local/bin/test.sh $?
with test.sh:
RESULT=$1
if [ $RESULT -gt 0 ]
then
echo "something bad happened and ended with result $RESULT" >&2
else
echo "everything is fine, sleep on"
fi
You can pass the exit code as a parameter to your script:
ls -l /tmp/filethatdoesnotex.ist
/usr/local/bin/test.sh $?
with test.sh:
RESULT=$1
if [ $RESULT -gt 0 ]
then
echo "something bad happened and ended with result $RESULT" >&2
else
echo "everything is fine, sleep on"
fi
answered Jun 6 at 12:00
Gerard H. Pille
1,073212
1,073212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448176%2fcron-jobs-monitoring-using-exit-code%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password