2

I've written a python script to connect to mysql & insert the current time then close. The use case is if my Pi browns out/restarts (often does) the time of this event will be logged. The script works just fine when called in command line.

I put the command: 'sudo python /home/pi/UpTime.py' at the end of the /etc/profile script, but on sudo reboot I get the error: 'No Module named mysql.connector'

This makes me think I've put the script to execute UpTime.py before whichever script boots mysql.

How can I further diagnose, or in which folder/boot script should I put the .py execution command?

  • 1
    Take a look at some other answers here on using the @reboot facility in cron for some ideas. Here's one that addresses some of your questions. – Seamus Jun 12 at 8:11
  • A python guru will no doubt correct me but isn't that specific error more to do with python being unable to find and load the MySQL libraries which suggests to me that it's actually a problem with your $PATH and other environment variables. This might make sense as that's part of the job that /etc/profile does (as @Milliways points out): set up the environment for the shell. – Roger Jones Jun 12 at 9:48
  • My next attempt was following Seamus's suggestion - editing the crontab editor (as per link in comment above). This worked just fine. I added the @30s delay to ensure all processes had started. If I knew how I'd subtract 30s from my datetime insert but for now, it's solved. Thanks – Brian Jun 14 at 11:45
2

As noted in a comment on your question, the error suggests that a Python module is not found, which is controlled in part by environment variables such as (but not limited to) PYTHONPATH. (See, for a full description, the documentation for Python modules.)

Note that this is different than saying your script runs before MySQL starts. That may or may not be true, but it does not seem relevant to the error that you're currently reporting.

Exactly why the path is not set when your script runs, we won't be able to see without seeing the files. Because of where you put your script though, it seems reasonable to think that you either called your script at a point in the profile before the Python-related paths were configured or that it's somehow being run by a different user or in a different environment (e.g. as happened with cron jobs) than your regular user shell.

1

Now you have your script running as a systemd daemon, you can review its status using journalctl. You don't even really need to write your startup message to a file as journalctl will show you every time the service starts...

But of course you can see startup by inspecting the contents of /var/log/syslog , and dmesg will also be useful.

1

/etc/profile is NOT for starting user code. It is:-

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

i.e. it is for configuring your shell (even if you wanted to do this you should probably use ~/.bash_profile or ~/.profile)

There are many ways of running a script on boot (depending on what you want to do). Often rc.local is used, but has drawbacks, not all programs will run reliably as not all services may be available when rc.local runs.
See systemd to have a command or program run when the Pi boots.

Without seeing your script, it is impossible to say, but a systemd Unit can be configured to wait for dependencies.

NOTE to start on boot you need sudo systemctl enable myscript.service

  • Thanks Milliways, I followed the systemd link you provided. When testing the script with sudo systemctl start myscript.service it works as expected, but when restarting the pi with sudo reboot it sends the system start time message to the DB 5-11 times over a roughly 1 minute timespan. – Brian Jun 12 at 8:22
1

The example you used at systemd is a bad example. It states to Restart=always. That doesn't help to "fix" a buggy service that failed to run stable. It will only spam your system with starting attempts until it will give up after some minutes.

In particular for your case it doesn't fit because you need to run your service only one time at start up and do not need to be in the background and work continuously as a service/daemon. I suggest to try this Unit File that you create with:

rpi ~$ sudo systemctl --full --force edit UpTime.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Up Time logging
After=mysql.service

[Service]
Type=oneshot
RemainAfterExit=yes
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/UpTime.py

[Install]
WantedBy=multi-user.target

I don't know the real name of the mysql service. I have named it in this example mysql.service. You have to use the correct name from your system. In general you can just look with ~$ systemctl what services are running. Enable your service with:

rpi ~$ sudo systemctl enable UpTime.service

and try with a reboot. It may not work on the first attempt because there are some additional environment settings are needed. We will look then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.