Shell script to run task and shutdown after task is done

Issue

I want to run a shell script that runs a python program and shutdowns after the program is done. Here is what I wrote

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

This just shutdowns the system without waiting for the program to complete. Is there a different command to use that waits for the program to complete?

Solution

What you have in your example should actually only shutdown once the python command has completed, unless the python program forks or backgrounds early.

Another way to run it would be to make the shutdown conditional upon the success of the first command

python command && sudo shutdown -h now

Of course this still will not help you if the python program does anything like forking or daemonizing. Simply try running the python script alone and take note if control returns immediately to the console or not.

Answered By – jdi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published