feldeOne
feldeOne

Reputation: 417

How to keep run Python Program in Background on Raspberry Pi after terminate ssh


i need to keep run my Python Program in Background on my Raspberry Pi after i close the ssh connection, Because i need to save Phidget information on a SQL DB
I try to do this with nohup but it seems that the python Program isn't even executed.

Because when I look into the MySql DB , after doing below there nothing inserted.

I type in :

    pi@raspi ~/MyProjekt $ sudo nohup python sensorReader.py &
    [1] 8580

and when i try to look if this process exist whit :

ps -A| grep 8580

it returns nothing.
So do i something wrong ?
How can i run the python program after close SSH Conneciton

Upvotes: 0

Views: 9988

Answers (2)

Nonnib
Nonnib

Reputation: 468

I would recommend running your python program in a cron reboot job.

To edit your root cronjobs use

sudo crontab -e

And add the line

@reboot sudo python full_path/MyProjekt/sensorReader.py

Then reboot your pi with:

sudo reboot

And then confirm that your process is running:

ps -aux | grep python

Upvotes: 3

Oberix
Oberix

Reputation: 1141

I don't think this is an ssh connection issue, from what you say the program seems to execute and exit. Does your .py execute in an infinite loop? Else you shouldn't expect it to stay alive.

Then, about keeping a process alive after the parent has terminated (the shell in your case), nohup is the answer, that means ignore HUP signals (those sent by a terminating parent process).

The '&' just means 'execute in background'.

The cron solution is good if your program is meant to do something periodically, but if it should stay alive waiting for some event (like listening to a socket), I would prefer to create an init scritp, so that the program is run as a demon at boot time and only in the desired runlevels.

Upvotes: 1

Related Questions