Start script on boot and see its output through SSH


8 posts
by raspi-config » Thu Dec 27, 2012 5:45 pm
Hi guys,

Is it possible to start a python application (made by me) on boot (using Debian Wheezy) and then see its output (the result of the "print" command) throught SSH if I want?

That application runs for a considerable amount time, and I want to be able to see its output to check if it is running in the correct way. But one of the requisites is that it should start on boot. I thought in lots of possibilities, but I'm "new" to Linux. Is there a way to start the "screen" program (screen is a well known terminal emulator that allows you to use multiple "bashes" by using only one SSH connection), and then start my application and make it runs on that "screen" instance?

I don't want to use any graphical interface (like LXDE). My Pi is supposed to run as server :).

Thank you for your attention!
User avatar
Posts: 20
Joined: Mon Dec 24, 2012 5:46 pm
by autotravis » Thu Dec 27, 2012 7:29 pm
So there are a few ways to do this. Here is one, using crontab.

On your pi (through ssh or a terminal) type sudo crontab -e and hit enter. Use nano if it asks you. Now hit the down arrow until your are below all of the text and type this:

@reboot python /path/to/script.py >> /path/to/logfile.log 2>&1

This will run your script at startup and log its printed output to the logfile mentioned. Press Ctrl+o to write out the crontab and then Ctrl+x to exit.
Things I do with my Raspberry Pi -> http://magnatecha.com/things-i-do-with-my-raspberry-pi/
User avatar
Posts: 5
Joined: Thu Dec 27, 2012 7:05 pm
by Joe Schmoe » Thu Dec 27, 2012 7:37 pm
Lose the sudo when editing your crontab. It is unnecessary and destructive.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2506
Joined: Sun Jan 15, 2012 1:11 pm
by autotravis » Thu Dec 27, 2012 7:47 pm
Joe Schmoe wrote:Lose the sudo when editing your crontab. It is unnecessary and destructive.

Wasn't sure if the @reboot would work in a regular user's crontab.
Things I do with my Raspberry Pi -> http://magnatecha.com/things-i-do-with-my-raspberry-pi/
User avatar
Posts: 5
Joined: Thu Dec 27, 2012 7:05 pm
by Joe Schmoe » Thu Dec 27, 2012 9:21 pm
autotravis wrote:
Joe Schmoe wrote:Lose the sudo when editing your crontab. It is unnecessary and destructive.

Wasn't sure if the @reboot would work in a regular user's crontab.


You may have a point there; I've never actually played with/tested the @reboot thingie.

Still, there's nothing in "man 5 crontab" that suggests that it wouldn't work for an ordinary user.

Nor is there any security reason I can see for it to not work (as long as the started task runs as the user, of course, not as root. This is normal behavior for crontab jobs, of course.)

FWIW, I've seen people in the past recommend using sudo for most any command that is even vaguely "system-y", including in particular working with cron and crontab files, and I think it is bad practice to do so.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2506
Joined: Sun Jan 15, 2012 1:11 pm
by raspi-config » Fri Dec 28, 2012 2:07 am
autotravis wrote:So there are a few ways to do this. Here is one, using crontab.

On your pi (through ssh or a terminal) type sudo crontab -e and hit enter. Use nano if it asks you. Now hit the down arrow until your are below all of the text and type this:

@reboot python /path/to/script.py >> /path/to/logfile.log 2>&1

This will run your script at startup and log its printed output to the logfile mentioned. Press Ctrl+o to write out the crontab and then Ctrl+x to exit.



Thanks for you help, but I can't use your suggestion. One of the pre-requisites (I didn't mentioned it because I didn't think about printing my program output to a file) is that I can't do writing operations on disk. Actually, I wrote my code in a manner that it avoids disk writes. All is done in the RAM memory, and when I need a file operation, it uploads/downloads the data to a web server without writing things in the flash card (it's kinda "cloud solution").

Besides, that program is supposed to run for hours, and the output is very, very large! I just want to see a piece of it when needed. I thought there was a way in Linux to start an application on system boot and get its output in the bash.
User avatar
Posts: 20
Joined: Mon Dec 24, 2012 5:46 pm
by jojopi » Fri Dec 28, 2012 4:14 am
raspi-config wrote:Is there a way to start the "screen" program (screen is a well known terminal emulator that allows you to use multiple "bashes" by using only one SSH connection), and then start my application and make it runs on that "screen" instance?
Of course. One possibility would be to add this line to /etc/inittab:
Code: Select all
py:2345:respawn:su - pi -c 'screen -D -m python'
This starts a python in a screen at boot time, but does not display it. Later you can log in as pi (locally or via ssh) and attach it with "screen -x". This example is an interactive python shell, but you can also add the path to a specific script.

When python finishes, screen exits and then init starts a new screen with a new python. If you do not need the respawn functionality then you can use "crontab -e" instead, which is simpler and more portable and does not require root privileges:
Code: Select all
@reboot screen -d -m python
User avatar
Posts: 1945
Joined: Tue Oct 11, 2011 8:38 pm
by raspi-config » Sun Jan 06, 2013 12:35 am
Thank you!!! It worked nicely! :D
User avatar
Posts: 20
Joined: Mon Dec 24, 2012 5:46 pm