0

I run a script through the terminal created via Bitvise SSH Client that collects data with an accelerometer. Now I was wondering if it's possible to completely shut down the terminal and connection to the Raspberry Pi from the PC while still having the script running on the Raspberry Pi itself. So when I access the Raspberry Pi again the script would still be running.

  • Do you need to interact with your script or do you just kick it off and it does stuff on its own? – tobyd Jan 18 '18 at 14:01
  • To begin with I just want to kick it off. – user75374 Jan 18 '18 at 14:04
1

On the assumption you are using Raspbian Jessie or Stretch (although this applies to Arch ARM) creating a systemd unit is probably what you are after. This acts like a service and lets you kick off a process in the background that can churn away doing things without needing you to be there.

You'll need to configure the unit file to your own script but under the assumption its a python script run from /home/pi/myscript.py and you want it to run from boot then the following should get you started.

sudo nano /etc/system/systemd/user75374.service

in the editor add the following

[Unit]
Description=My rpi.se service

[Service]
ExecStart=/usr/python /home/pi/myscript.py

[Install]
WantedBy=multi-user.target

then save via ctrl-x

then enable and start via
sudo systemctl enable user75374.service
sudo systemctl start user75374.service

This acts to enable your service to run (as root!!) when the system reaches multiuser mode (after its done its basic setup). It will invoke python and your script in the background and just run it until it blows up or stops. It won't restart it or do anything else. If your script writes to the console it'll instead end up in the log accessible under sudo journalctl -u user75374.service. You'll almost certainly need to adapt the unit file to however you run your script. If you update the unit after you've enabled it then you'll need to let the system know via sudo systemctl daemon-reload. Gory details of the unit options here.

0

This sounds like you are logging in to a terminal via ssh, running something and you want to be able to disconnect from ssh and then come back later to see it still running? If so, the tool you want is screen.

An example usage would be:

> screen -S screen1
> myprogram
# ... program output as it runs
CtrlA - D
[detached from screen]
> screen -ls
> screen -r screen1

This would create a new named screen 'screen1' which you can resume later by using 'screen -r'. 'screen -ls' will show any active screens you have. If you 'man screen', you can see all the key bindings you can use to interact with a screen session (same info is available on the web doc link above).

The key binding I use most is 'CtrlA - D' which means to press CtrlA, let go, followed by pressing D. This will detach from the screen, which leaves the session running in the background. You can reconnect at any time, even if you disconnect ssh and login from another session later.

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.