How to auto run python script in Ubuntu or in RPi OS

I am wondering how we can auto run the python script in Ubuntu or in Raspberry Pi OS upon every restart. We would like to run a python script automatically after the reboot. How can we implement this in Ubuntu or on RPi OS.

One way can be to use the cron daemon specifying the time for the task as @reboot.

1 Like

Other than cron, I’m not sure what the best trigger to pick is for your situation, but a SystemD service is a perfect fit. The unit files are very readable.

FYI, an old school simple way to run a command when a user logs in, is to add it to .bashrc.

1 Like

Simple way to get into trouble. Too many ways that hack can blowup in your face.

Systemd is the robust way to run a command after boot or when the user logs in.

2 Likes

Is .bashrc still OK to customise shell sessions?

I’d recommend against .bashrc, which runs every time you open a terminal (making it good for customizing shell sessions) but might not be what they want. In addition to cron and @reboot, you can use systemd by creating a .service file with info for running the script. I don’t have a Raspi on hand, so I can’t verify specifics and different websites are giving me different info (probably AI slop articles).

cron might be the simplest way.

I am not sure which way to go. There are several replies.

I read about “cron is a shell command for scheduling a job to run periodically at a fixed time, date, or interval. As scheduled, it is known as a cron job“.

Do we really need date and time ? How about if we don’t want to specify date and time to run the script.

After rebooting, the Raspberry Pi OS should run the python script, few second delay is acceptable.

@reboot is what would be written instead of a time.

You run crontab -e, for example. Then edit the user’s crontab file and add a line like

@reboot <user name> <path/to/python> <path/to/your/script.py> <other arguments>
1 Like

I am now trying to run a python script in virtual machine which is running under windows. I have edited the file by running crontab -e. I have written the following line at the end of the file and save it.

@reboot <path/to/python> <path/to/your/script.py>

Nothing comes when I reboot the virtual machine.

Windows is very different to Ubuntu and RPi OS, unless you mean a Windows host, running a Linux VM within that, e.g. Windows Subsystem for Linux?

On Windows itself, look into scheduled services. There’re various libraries on PyPi.

What is it that you are expecting here? Are you expecting to see some printed output or something?

A cron job does not have its stdout connected to any terminal:

It’s the place to add things like aliases.

It is not the place to run one-of-commands. This is becuase
.bashrc is run for every new sub-shell you create.

1 Like

If you want to see the output then you will need too be logged and then use your desktop, Gnome, KDE, etc, to start the script running in a terminal.

If you want to check the output then I recommand you use a systemd service as the output of the service will be in the system or user journals.

I am using Virtual Machine in which I have Ubuntu installed. This is not Windows Subsystem for Linux.

In the python program which I wanted to auto run upon reboot, I have a few print statements with some delay in between that should appear on the terminal. The terminal does not open it’s self one the reboot is complete and Desktop appears.

Booting the machine is different to logging in and creating a terminal session on that machine (you can have multiple sessions).

I don’t know how to view the stdout from a cron job, but if you use a systemd service instead, it’s all there (eventually) by running journalctl.

Make sure the same VM really is being rebooted too. If destroying the VM and creating a new one entirely (like I do), then to run one off custom config, including setting up a script to run on each boot, the actual image needs altering, or e.g. a cloudinit file needs passing in to the VM creation tool.

Thanks. I will have a look at systemd service.

I made some changes in the python program for RPi CM4 board. The python program is now just blink led program on gpio pin of RPi CM4. The blink led python program is working and tested on the board when the python program is running directly in terminal without cron. I would like to test the auto run python program with cron. Kindly let me know if this command is correct. “@rebootreboot python3 /home/cm4-guest/script.py>”. I added at the end of the file open with crontab -e and reboot but the python program does not work as I don’t see the blink led on the gpio pin.

In a SystemD service unit (sorry for going on about it) the python3 would need to be /usr/bin/python3 or the absolute path to whichever Python binary it’s to be run with (e.g. one from a venv).

I tried “@reboot /usr/bin/python3 /home/cm4-guest/script.py>” in crontab -e. This does not work in RPi CM4.

You could check the file /var/log/syslog to see if there are any error messages from when the cron task is run, if it is run.