Python – A shell script that executes a task and closes after the task completes

A shell script that executes a task and closes after the task completes… here is a solution to the problem.

A shell script that executes a task and closes after the task completes

I want to run a shell script that runs a python program and closes when the program is finished. This is what I wrote

#!/bin/bash
python program
sudo shutdown -h now

This simply shuts down the system without waiting for the program to finish. Are there other commands available that wait for the program to complete?

Solution

The content in your example should actually only be closed after the python command completes, unless the python program is forked ahead of time or running in the background.

Another way to run it is to make the shutdown conditional on the success of the first command

python command && sudo shutdown -h now

Of course, if a Python program performs something like forking or daemons, this still won’t help you. Just try running the python script separately and note that the control returns to the console immediately.

Related Problems and Solutions