pminogue
Posts: 3
Joined: Tue Jan 29, 2013 9:04 am
Location: Cambridge, UK

Starting programs before login

Tue Jan 29, 2013 9:15 am

Hi, first post here, owned a Pi since they were released but only had time to get into it with the past few weeks! Moderately experienced programmer and circuit designer.

Little question I've had for a while about the Pi. I see some people's projects online in which there is no need for a keyboard for their project to work, for example, using the Pi as an infotainment manager in a car; the Pi boots up when the car starts and begins to handle the car's audio.

I've done quite a bit of Python with the Pi controlling external devices but I always need to login in on boot up, how can I start the Pi, with no keyboard, mouse or monitor and have the Pi start performing tasks on its own? Is this OS dependent? Is there a good guide anywhere on doing this?

Thanks in advance for any help :-)

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: Starting programs before login

Tue Jan 29, 2013 9:56 am

Your first place to look is ssh this is remote login from your main computer If you are on Windows or Linux the Putty is a good start point.

Programs that run in the background at startup and before login are controlled by Deemon processes there's some general info here http://en.wikipedia.org/wiki/Daemon_%28computing%29. ;)

It's worth a little time getting your head around this.
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

pminogue
Posts: 3
Joined: Tue Jan 29, 2013 9:04 am
Location: Cambridge, UK

Re: Starting programs before login

Tue Jan 29, 2013 10:16 am

Thanks for the response, I'll look into those topics!

Without wanting to sound sceptical, I'm not sure that remote logging in is what I'm after. Suppose I want to use the Pi in my car to control the infotainment system and want the Pi to start as soon as I turn the ignition, it's not going to feel seamless if I have to boot up my laptop every time I start the car and log in. I'd like the Pi to start up and start doing its work without being stuck at a log in screen and requiring me to use a keyboard.

Maybe you have pointed me in the right direction, I'll look into it thoroughly after work!

User avatar
RaTTuS
Posts: 10574
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Starting programs before login

Tue Jan 29, 2013 10:23 am

if you have made a program , or script or other code that works fine , you can add that to
/etc/rc.local
before the
exit 0
and that will run as root before you logon
you will need to add full paths to the script etc.

alternatively you can make the RPi log you in automagically on startup and even get your bash login script to run what you want on a specific terminal
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: Starting programs before login

Tue Jan 29, 2013 10:28 am

I was just pointing out that it's a good example of a programe that runs before login.

One that you can explore to test your syatem.

If you have an HDMI monitor plugged in waiting at login and you ssh into your RPi with Putty and ssh run startx the X Window opens on your HDMI not your Putty.

I have also used an ssh client from my android mobile phone to link into my RPi using ConnectBot. ;)

There's loads of options with Linux :lol:
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

pminogue
Posts: 3
Joined: Tue Jan 29, 2013 9:04 am
Location: Cambridge, UK

Re: Starting programs before login

Tue Jan 29, 2013 10:33 am

Right, thanks guys, think ye may be pointing me in the right direction now. Will play around with that at the weekend and see how it goes.

Thanks :-)

Glochief
Posts: 1
Joined: Tue Jan 29, 2013 12:25 pm

Re: Starting programs before login

Tue Jan 29, 2013 12:52 pm

Hi,

I powered up my Raspberry pi for the first time this afternoon and have successfully built wx200d on the Raspberry Pi for my first application.

http://sourceforge.net/projects/wx200d/?source=navbar

I now have two daemons that I wish to start at boot time to log my met system and upload the observations to wunderground using wx200d

It is all running ok by executing a command from the shell to start the wx200d daemon and starting a cron job for the wunderground upload. However now I want to start them both as daemons at boot time. Here are the two commands.

wx200d -s /dev/ttyUSB0 -w /home/pi/log
(sleep 60 && /usr/local/sbin/wunderground 15 /path/to/idpasswdfile) &

Is it OK to simply put these two lines into /etc/rc.local or is there a more appropriate place to put them? I have attempted to read section 9.3 in http://www.debian.org/doc/debian-policy ... s-sysvinit but it is pretty heavy going.

BTW could someone parse/explain the syntax of the second command:
(sleep 60 && /usr/local/sbin/wunderground 15 /path/to/idpasswdfile) &

Any help would be greatly appreciated.

Regards,
Chris

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Starting programs before login

Tue Jan 29, 2013 1:32 pm

( ... ) &
Means run all this in the background and don't wait for it to finish

sleep 60
means wait a minute

&&
means, wait for the previous command to finish, and if it succeeded, run this next one.

/usr/local/sbin/wunderground 15 /path/to/idpasswdfile
means run this command with these parameters

User avatar
IgorGanapolsky
Posts: 33
Joined: Sun Apr 10, 2016 8:32 pm

Re: Starting programs before login

Sun Aug 07, 2016 9:57 pm

RaTTuS wrote:if you have made a program , or script or other code that works fine , you can add that to
/etc/rc.local
before the
exit 0
and that will run as root before you logon
you will need to add full paths to the script etc.

alternatively you can make the RPi log you in automagically on startup and even get your bash login script to run what you want on a specific terminal
I followed this approach for starting Ngrok. Unfortunately, it doesn't start until AFTER I login.

User avatar
RaTTuS
Posts: 10574
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Starting programs before login

Mon Aug 08, 2016 6:43 am

IgorGanapolsky wrote:
RaTTuS wrote:if you have made a program , or script or other code that works fine , you can add that to
/etc/rc.local
before the
exit 0
and that will run as root before you logon
you will need to add full paths to the script etc.

alternatively you can make the RPi log you in automagically on startup and even get your bash login script to run what you want on a specific terminal
I followed this approach for starting Ngrok. Unfortunately, it doesn't start until AFTER I login.
if you are using systemd then it may well run after you login - because of timing ...
rc.local is not the way to do things now IMO
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Heater
Posts: 16539
Joined: Tue Jul 17, 2012 3:02 pm

Re: Starting programs before login

Mon Aug 08, 2016 7:39 am

On jessie I use systemd to start my services.

Create a systemd service file. It will look a bit like this:

Code: Select all

[Unit]
Description=My Background Process.
After=network.target

[Service]
ExecStart=/path/to/MyBackgoundProcessExecutable
WorkingDirectory=/path/to/directory/MyBackgoundProcessExecutable/should/run/in
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyBackgoundProcess
User=root
Group=root
Restart=always

[Install]
WantedBy=multi-user.target
Put that in a file in /etc/systemd/system with a name like MyBackgoundProcess.service

Note how that has an option telling systemd to restart the service if it crashes.

Enable it to run at boot time with the command:

$ sudo systemctl enable MyBackgoundProcess

Stop and start it at will with:

$ sudo systemctl stop MyBackgoundProcess
$ sudo systemctl start MyBackgoundProcess


Google for systemd service for more information.
Memory in C++ is a leaky abstraction .

harrytheginger
Posts: 1
Joined: Wed Jan 16, 2019 12:46 am

Re: Starting programs before login

Wed Jan 16, 2019 12:49 am

Heater wrote:
Mon Aug 08, 2016 7:39 am
On jessie I use systemd to start my services.

Create a systemd service file. It will look a bit like this:

Code: Select all

[Unit]
Description=My Background Process.
After=network.target

[Service]
ExecStart=/path/to/MyBackgoundProcessExecutable
WorkingDirectory=/path/to/directory/MyBackgoundProcessExecutable/should/run/in
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyBackgoundProcess
User=root
Group=root
Restart=always

[Install]
WantedBy=multi-user.target
Put that in a file in /etc/systemd/system with a name like MyBackgoundProcess.service

Note how that has an option telling systemd to restart the service if it crashes.

Enable it to run at boot time with the command:

$ sudo systemctl enable MyBackgoundProcess

Stop and start it at will with:

$ sudo systemctl stop MyBackgoundProcess
$ sudo systemctl start MyBackgoundProcess


Google for systemd service for more information.
I just now created an account here to say you sir are a gentleman and a scholar.
seriously though I've been googling for hours this worked flawlessly

Heater
Posts: 16539
Joined: Tue Jul 17, 2012 3:02 pm

Re: Starting programs before login

Thu Jan 17, 2019 6:57 pm

harrytheginger,

Wow, what? After my posting nearly three years ago and forgetting all about it. Thank you for the compliment. Totally undeserved as it is.

It warms my heart that sometimes my ramblings on the net are of some use to somebody, somewhere.

All the best.
Memory in C++ is a leaky abstraction .

tonythetechie
Posts: 6
Joined: Sun Jan 20, 2019 9:46 am

Re: Starting programs before login

Sun Jan 20, 2019 9:55 am

Can I ask a supplemental question?

I have a systemd service script setup that starts my Plexdrive at startup.

The trouble is Plexdrive is dependant on FUSE being mounted, but FUSE is not mounting until after the Plexdrive service is started.

How do I go about controlling the order in which the Plexdrive service starts?

Return to “General programming discussion”