Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Python script within shell script - Check status

Within my shell script I run this command:

python script.py

I was wondering, as a two part question:

  1. How can I program my python script to pass a status back to the shell script that ran it depending on what happens in the python script. For example if something goes wrong in the python script have it exit with a code of 1 which is sent back to shell.

  2. How can I get my shell script to read the exit code of python and exit for an error? For example, a status code of anything but 0 then exit.

like image 991
Jimmy Avatar asked Jan 21 '13 21:01

Jimmy


People also ask

How can I tell if my Python script is running?

I usually use ps -fA | grep python to see what processes are running. The CMD will show you what python scripts you have running, although it won't give you the directory of the script.

How do you check if a shell script is executed successfully?

Using Special Variable $? The shell treats several parameters as special variables. The $? special variable expands to the exit status of the last executed command. By comparing that value with the expected one, we can check whether the command has been executed successfully or not.

How do you check if a script is already running?

An easier way to check for a process already executing is the pidof command. Alternatively, have your script create a PID file when it executes. It's then a simple exercise of checking for the presence of the PID file to determine if the process is already running. #!/bin/bash # abc.sh mypidfile=/var/run/abc.

How do I run a Python program in shell script?

Running a Script Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T . Navigate the terminal to the directory where the script is located using the cd command. Type python SCRIPTNAME.py in the terminal to execute the script.


2 Answers

First, you can pass the desired exit code as an argument to sys.exit in your python script.

Second, the exit code of the most recently exited process can be found in the bash parameter $?. However, you may not need to check it explicitly:

if python script.py; then
    echo "Exit code of 0, success"
else
    echo "Exit code of $?, failure"
fi

To check the exit code explicitly, you need to supply a conditional expression to the if statement:

python script.py
if [[ $? = 0 ]]; then
    echo "success"
else
    echo "failure: $?"
fi
like image 114
chepner Avatar answered Sep 20 '22 21:09

chepner


Hate to resurrect a dinosaur but while this selected answer worked as written I wanted to add a variation where one can check against multiple exit codes. $? only seems to retrieve the exit code of the last executed program one time. So if you need to check the exit code against multiple cases, it is necessary to assign $? to a variable and then check the variable, a la (using @chepner 's example):

python script.py
exit_code=$?
if [[ $exit_code = 0 ]]; then
    echo "success"
elif [[ $exit_code = 1 ]]; then
    echo "a different form of success, maybe"
else
    echo "failure: $exit_code"
fi
like image 30
Beez Avatar answered Sep 23 '22 21:09

Beez