chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

A simple need to run a command on boot!

Wed May 15, 2013 8:31 pm

For someone who grew up with MS-DOS (I had no choice), I'm am asking myself why I should find myself pulling out my hair with the most modest and humdrum of requirements.

I need to start a program automatically on boot. That program is Motion. I can start this program by typing it into a SSH session after boot. Easy. It works. It surely should be simple to get Raspbian to do the same. This must be one of the basic user requirements of an operating system.

Alas, I fail. There is nothing as simple as Autoexec.bat. There are instead a number of script files (rc.local) and several hacks using cron etc; none of which are intuitive.

But wait, adding the same command that I would type into Bash to start Motion, into rc.local sounds intuitive. But it doesn't work. Why? Well, perhaps I could look at the boot log file (since I'm running headless).

Oh. The boot.log file in var/logs is empty. No record of past boot messages. How am I to figure this one out? I've looked at other logs in this directory. Nothing immediately useful.

I really want to get into Linux. I love the RPi. And yes, the challenge is fun. But now I just want to achieve a particular task and I am disappointed how, for a newbie, this needs to be so difficult. I appreciate the learning curve, but I'm close to calling it a day. How must others, especially those who have never delved into a command line, cope?

In summary my need is this: I would like the RPi to boot straight into Motion, running as a daemon. Motion is not the problem here because it runs perfectly well when I type it manually, and I've configured it to run as a daemon so I can close the SSH session in which I started the instance. It streams. It streams pretty well (12% CPU usage at 640x480). I can't, however, get the RPi to successfully mimic my fingers bashing the keyboard without my intervention. A basic expectation, perhaps.

If anyone can help I would be very appreciative: I thank you all in advance.

SimonSmall
Posts: 220
Joined: Tue Oct 09, 2012 8:13 pm

Re: A simple need to run a command on boot!

Wed May 15, 2013 9:00 pm

Don't expect the Pi, or Linux, to run like windows. It has its own way of doing things, freely available on the internet. You may have problems if you don't know what to look for, but then you can ask a question here and you should get an answer if people know what you want

The boot messages are shown by typing "dmesg" or if you need to read it all "dmesg | less", then press the space bar to see each page

Where have you installed Motion? You don't say what you typed to get it to run from the command line, but you probably need to put the full path in rc.local so the Pi boot sequence knows where to find it

chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Re: A simple need to run a command on boot!

Wed May 15, 2013 9:24 pm

Hi and thanks for your reply.

Yes, I thought after I posted that I should have included the following:

To run from a SSH session I'm typing "sudo motion"; it needs sudo because of the motion.conf file located in /etc/motion. It works. My assumption is that the RPi should simply be able to replicate this command automatically at boot if encapsulated in rc.local

The only reference I can find to Motion within the directory structure is within "/etc/init.d". However, if I execute "sudo /etc/init.d/motion start" I get an error (not starting motion daemon, disabled via /etc/default/motion). It does start the daemon using "sudo motion".

I've checked, and the daemon is enabled in etc/default/motion.

Zxa
Posts: 7
Joined: Fri Sep 14, 2012 3:44 pm

Re: A simple need to run a command on boot!

Wed May 15, 2013 9:48 pm

I also had this problem and found the following solution: Type 'crontab -e' into the terminal, and add a new line at the bottom starting with @reboot followed by your command:

Code: Select all

@reboot sudo motion
I'm also fairly new to Linux, so there could easily be a better way of doing this, but it works for me.

User avatar
jojopi
Posts: 3424
Joined: Tue Oct 11, 2011 8:38 pm

Re: A simple need to run a command on boot!

Wed May 15, 2013 10:14 pm

chorlton2080 wrote:However, if I execute "sudo /etc/init.d/motion start" I get an error (not starting motion daemon, disabled via /etc/default/motion). It does start the daemon using "sudo motion".

I've checked, and the daemon is enabled in etc/default/motion.
I suggest you check again. That message is not displayed if start_motion_daemon=yes, but no other spelling or capitalization will do. And because /etc/default/motion is really shell commands rather than a configuration file, it is essential that there are no spaces before or after the =.

Once the daemon is not disabled it should start automatically at boot, so you do not need to hack with rc.local.

chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Re: A simple need to run a command on boot!

Thu May 30, 2013 8:07 pm

A good tutorial on running scripts on startup:

http://www.stuffaboutcode.com/2012/06/r ... rt-up.html

And, from the same author, how to get the RPi to boot into timelapse mode:

http://www.stuffaboutcode.com/2013/05/t ... ry-pi.html

User avatar
perfo
Posts: 102
Joined: Mon Jan 30, 2012 2:42 am

Re: A simple need to run a command on boot!

Sat Mar 29, 2014 5:28 pm

Hello, all I'm in the same boat as the folks above.
For some reason I can't get the Picam mmal motion to run at start up. (not even with the cron method)

I've followed the very helpful and clear guide in setting up the PiCam to work with motion.
http://rbnrpi.wordpress.com/project-lis ... etect-cam/

and I've added the stop and start scripts as suggested in the scripts and made them chmod 755
If I go into the /home/pi/mmal directory and type
./startmotion or ./stopmotion is works perfect.

if I go to my root directory and type

/home/pi/mmal/startmotion no error come up but nothing happens so I guess this is where my problems are in getting it to auto start.

Unfortunately the link given on this page for the tutorial didn't help unless I missed something. I am a newby with Linux so quite expecting it to be a basic error but I have been at this thing for quite a while and getting nowhere. I'm using the Wheezy OS

Any assistance would be most appreciated. Thanks.

User avatar
perfo
Posts: 102
Joined: Mon Jan 30, 2012 2:42 am

Re: A simple need to run a command on boot!

Sat Mar 29, 2014 7:43 pm

Ok, I've bottomed it. It was silly mistake on my behalf.
I edited /etc/rc.local

and added the line

sleep 4
sudo /home/pi/mmal/startmotion &

The sleep seems to be needed due to ...well I'm not really sure but it appears if you are too quick it can't find some of the folders. It's like the Home folder gets assigned at run time ????

I also changed the startmotion to
#!/bin/sh
nohup /home/pi/mmal/motion-mmal -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &

(The above is meant to all be on one line)
So now I've included the full path to the motion program and the motion config file. I'm pleased to say it now boots up nicely...
During boot up it gives a warning that motion demon is not started. This doesn't affect the running but seeing as it is set to no in the conf file I guess there must be another motion conf file somewhere.
Anyway I hope my fumblings will help someone.

User avatar
DougieLawson
Posts: 40820
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: A simple need to run a command on boot!

Sat Mar 29, 2014 8:06 pm

Why not just add
sleep 4
/home/pi/mmal/motion-mmal -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
to /etc/rc.local

Forget having the intermediate script.

You don't need sudo, rc.local runs as root.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
perfo
Posts: 102
Joined: Mon Jan 30, 2012 2:42 am

Re: A simple need to run a command on boot!

Sat Mar 29, 2014 8:12 pm

yes that worked as well but I'd already made the start and stop scripts from the previous set up guide so I used them as I knew they worked...

Return to “Raspberry Pi OS”