It's hard to summarise my question in the title, so here is the actual situation. I am running picam
to use my camera module as a security camera. I don't need it when I'm in the house, so before I leave I SSH into my Pi, start my web server (which hosts images that picam generates if motion is detected) and run python picam.py
. The terminal is then running the script - I have to Ctrl + C out of it to stop it.
If I SSH in later, I am in the terminal again, but one in which the script isn't running. Is the script still running in a separate terminal in memory? Does it stop when I SSH in again? Can I access that running terminal? What happens if I run the script again from the new terminal?
2 Answers 2
Install screen or tmux (both are available in raspbian repos). First start screen or tmux from your first ssh session, and start other programs from a screen or tmux window. You can then quit your first ssh session. Your programs will continue to run. You can later open another ssh session and reattach to the first tmux window.
-
I've tried this, but perhaps I'm doing something wrong. I ssh in, run tmux, run picam.py, then quit out. When I go back in with a new SSH, I run tmux again, but it's just a new session. Any windows I created in the first tmux session are lost.user8543– user85432014年07月16日 16:45:49 +00:00Commented Jul 16, 2014 at 16:45
-
2use
ctrl-b d
to detach andtmux att
to attach to the existing session.Craig– Craig2014年07月16日 18:10:53 +00:00Commented Jul 16, 2014 at 18:10 -
Never mind, I've worked it out. For future reference, tmux opens separate sessions which can be listed with tmux ls. You can attach (enter) to a session with tmux a -t <number>user8543– user85432014年07月16日 18:14:36 +00:00Commented Jul 16, 2014 at 18:14
Yes, the script is still running som the session you had - no, it does not stop when you open a new connection. You can't really access that session from another one, but you can manipulate what it's doing - try ps -e
. If you run the script in the new session you will start a new instance of the script - exactly what will happen in this case is hard to tell. Try it.
What you want might be to create a service - that is one instance of a script that runs and can be started or stopped from different places. Have a look at http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts