How to Initiate a Program Automatically Upon System Startup?
Mrinal Raj
AI Engineer building Cisco Software | Speaker - PyCon | CCNA & DevNet Certified | SDE-2 | Co-Founder, Scriptink | Bangalore
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
sudo su -
crontab -e
@reboot /usr/bin/python3 /path/to/your/script.py
Option 2: Editing through services using systemd
chmod +x myscript.sh
cd /etc/systemd/system/
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
sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo systemctl start myscript.service
sudo systemctl status myscript.service
Option 3: Autostart using lxsession
mkdir -p ~/.config/lxsession/LXDE-pi/
nano ~/.config/lxsession/LXDE-pi/autostart
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e python3 /path/to/your/script.py
sudo reboot
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/?