Start a program on startup using linux crontab

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Scope

Would you like your program to automatically start when your Robot boots? Then ‘crontab’ is the solution for you!!!

Application

Raspberry Pi, Nvidia Nano

Linux

Python 3

C++

Pre-requisites knowledge

Linux CLI

Mobaxterm

Neopixels installed

Know you execution command

For this example, we want our Neopixel lights to start on bootup. The linux command to start the program is:

sudo python3 strandtest2.py -c

Open the Crontab file

From the linux command line interface (CLI), enter the following command.

crontab -e

You may need to answer some questions if this is the first time you’ve used crontab. Choose ‘1’ to use Nano.

When you’re in nano editing the crontab file, add the commabd line at the bottom.

The syntax is <@reboot><your program command>

See below for an example.

To save and exit, press ‘Ctrl + X’, then ‘y’ to save.

To activate, reboot the robot.

sudo reboot

Hot to stop a contrab program

From the linux command, enter the following command. It will show you all of the active programs.

ps aux

Now, identify your program from the list and record the PID number.

As you can see above, our program ‘strandtest2.py’ is listed twice. Once as a ‘root’ user and as a ‘pi’ user.

We want to stop the ‘root’ user program.

To do this, enter the following syntax; sudo kill <PID>

In our case, we would enter…

sudo kill 283

The ‘kill’ command will immediately stop the program. It is different to ‘Ctrl+C’.

In this example, if we exited the main program with ‘Ctrl + C’ the led’s would clear. Since we have killed the program, this command did not execute and the led’s kept their last command.

If you want the led’s to clear, you will need to run the program again and stop the program with ‘Ctrl + C’.

sudo python3 strandtest2.py -c

Did it work for you?

If this didn’t work, please message us or raise an issue on our github.

Contributors

Marty.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.