2

I created a python program, "test.py" and have saved it under /home/pi/. When I go to run it in the terminal using "python3 /home/pi/test.py" it runs properly and speaks "hello world". The code is shown below.

import os
import alsaaudio
m = alsaaudio.Mixer()
current_volume = m.getvolume()
m.setvolume(35)
os.system("espeak 'Hello World!'")

I want this program to start whenever my raspberry pi starts up. I tried to add this line in crontab but my raspberry pi doesn't execute the command. Does anyone know why my program won't execute?

@reboot python3 /home/pi/test.py

Here is an image of the syslog crontab

asked Dec 25, 2020 at 3:33
6
  • 1
    Would you please provide the output of the log file /var/log/syslog... And provide the content of your crontab and for which user it is for. Thanks. Oh and the permissions of the file in question. Commented Dec 25, 2020 at 16:49
  • I have added an image of the logs. I ran crontab as a root user sudo crontab -e The only crontab I have set is the one i showed above. Thanks. I have already tried running the script with sudo in crontab but ti still didn't work. Commented Dec 25, 2020 at 17:23
  • Try putting it under pi's crontab. Commented Dec 26, 2020 at 8:07
  • Is this working for you? Commented Jan 7, 2021 at 13:42
  • No it didnt work even after putting it under the pi's crontab. I checked the status of the service and found the problem. It turns out that some of my imports weren't working in python. When my system tried to start the script, there were errors in the program so it would simply quit. So it seems like it wasn't a problem with crontab. It was just a problem with loading the libraries into my program Commented Jan 8, 2021 at 3:45

1 Answer 1

1

can you try adding the full path to python3:

@reboot /usr/bin/python3 /home/pi/test.py


Also, regarding wanting to run the code on when the device boots - you can run your code as a service.
To do so create a .service file under /etc/systemd/system (for example my-code.service)

Enter the following inside the file

[Unit]
Description=My python service
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u test.py
WorkingDirectory=/home/pi
[Install]
WantedBy=multi-user.target

Finally enable the service (in order for it to run on boot)

sudo systemctl enable my-code

If you want to run it independently you can also run

sudo systemctl start my-code
answered Dec 26, 2020 at 11:25
Sign up to request clarification or add additional context in comments.

1 Comment

I tried both of your suggestions and they didnt work. I checked the status of the service and found the problem. It turns out that some of my imports weren't working in python. When my system tried to start the script, there were errors in the program so it would simply quit. So it seems like it wasn't a problem with crontab. It was just a problem with loading the libraries into my program.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.