How to Initiate a Program Automatically Upon System Startup?
Wikipedia: https://upload.wikimedia.org/wikipedia/commons/7/7d/23551-raspberry-pi-5.jpg

How to Initiate a Program Automatically Upon System Startup?

Scenario

I recently wanted my Python program to execute right after the system boots up. I discovered several options to achieve this, eliminating the need for manual intervention to run the program.

Option 1: Editing through Crontab

  1. Open a terminal with elevated privileges:

sudo su -        

  1. Edit the crontab configuration:

crontab -e        

  1. Add the following command to start the Python program (assuming you want to run it on a Raspberry Pi terminal):

@reboot /usr/bin/python3 /path/to/your/script.py        

Option 2: Editing through services using systemd

  • Create a script, name it as “myscript.sh”
  • Make the script executable:

chmod +x myscript.sh        

  • Navigate to the systemd directory:

cd /etc/systemd/system/        

  • Create a service file named “myscript.service” inside the systemd directory.
  • And below lines using

nano myscript.sh        
Description=My Script

[Service]
ExecStart=/path/to/myscript.sh
WorkingDirectory=/path/to/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=<replace_username>

[Install]
WantedBy=multi-user.target
        

  • Reload systemd:

sudo systemctl daemon-reload        

  • Enable the service:

sudo systemctl enable myscript.service        

  • Start the service:

sudo systemctl start myscript.service        

  • To check the service status:

sudo systemctl status myscript.service        

Option 3: Autostart using lxsession

  • Edit the autostart configuration file:

mkdir -p ~/.config/lxsession/LXDE-pi/
nano ~/.config/lxsession/LXDE-pi/autostart        

  • Add the following line to automatically start the Python script:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e python3 /path/to/your/script.py        

  • Save the file and initiate a system reboot:

sudo reboot        
Mrinal Raj

AI Engineer building Cisco Software | Speaker - PyCon | CCNA & DevNet Certified | SDE-2 | Co-Founder, Scriptink | Bangalore

1 年

Link to complete Networking and Automation Tips for Beginners: Startup Programs, Static IPs, Free URLs, and Port Forwarding. Follow me for more 1. How to Initiate a Program Automatically Upon System Startup? : https://www.dhirubhai.net/pulse/how-initiate-program-automatically-upon-system-startup-mrinal-raj-aet9c? 2. How to Set a Static IP for Your Python Project? : https://www.dhirubhai.net/pulse/how-set-static-ip-your-python-project-mrinal-raj-zhmtc/? 3. How to get Free URL for your ever changing Router's Public IP? : https://www.dhirubhai.net/pulse/how-get-free-url-your-ever-changing-routers-public-ip-mrinal-raj-ijvhc/? 4. You won't believe – The Port Forwarding concept : https://www.dhirubhai.net/pulse/you-wont-believe-port-forwarding-concept-mrinal-raj-4hitc/?

回复

要查看或添加评论,请登录

Mrinal Raj的更多文章

社区洞察

其他会员也浏览了