I have a Bash script that requires some cleanup commands to happens when it gets closed in the console.

Basically the cleanup is a restart command in my Python daemon program (let's call this P), which stops the current process and starts a new one. The Bash script is used to monitor the Python daemon app.

However the problem is that for some reason, it's not launching the program to restart the Python program.

Bash script is located on the desktop of my Raspberry Pi using Raspbian. I am supposed to start this by double-clicking it and selecting Execute in Terminal.

Here is the gist of my code.

function clean_up {
    python3 A restart & #stops the python Deamon if running, then start it. (will start if it wasn't running to begin with)

trap clean_up SIGINT 
trap clean_up SIGTERM
trap clean_up SIGHUP
trap clean_up SIGCHLD
trap clean_up SIGCONT
python3 A stop
... (code to start monitor)
python3 A start ArgumentToHookToMonistor 
  • That bash script doesn't monitor anything - it disowns it. – mikeserv Oct 9 '15 at 0:06
  • disown was that so that "python3 A restart" would detached from the console, so it run in the background even when Terminal window was closed. Or atleast thats the idea. – user3346931 Oct 9 '15 at 17:05
  • You should (trap '' HUP; python3 restart <&3 >&3 2>&3) 3<>/dev/null – mikeserv Oct 9 '15 at 17:10

I am not sure this will do what you want, but it will help a little debugging the issue:

  • At the beginning of your script: enable job control (disabled by default in scripts) with: set -m

  • Then inside the clean_up function, change disown to disown %1 2>&1 | tee /path/to/logfile.log

  • Also in that function, add a sleep 3 before the quit line, if you want to have time to see messages while running the script manually.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.