2

I have a Python script running at boot, on a Raspberry Pi, and of course I didn't write down how I set it up. Now I need to change it, and I can't for the life of me figure out how it's getting launched. How can I figure this out?

I've searched for methods to launch a script at boot, in the hopes that I will stumble across the right one. For instance, Run a Program On Your Raspberry Pi At Startup gives several options, and I've gone through each of them, to walk through the setup, and see if I already changed the settings. None of them seem to be what I did.

  • /etc/rc.local is pretty empty
  • It's not in ~/.bashrc (and I wouldn't want it to be — I want it to run at boot, not when I log in)
  • As far as I can tell, it's not in /etc/init.d/, but I suppose the launcher script could be named something that I'm not expecting?
  • It's not set up as a systemd service (Again, as far as I can tell — maybe I named it something weird?)
  • There are no crontab entries

If I search running processes, I get this:

$ ps aux | grep python
root       452  0.0  0.0   1912   396 ?        Ss   12:18   0:00 /bin/sh -c sudo python /home/pi/tikinook/nook_controller.py
root       453  0.0  0.3   6968  3228 ?        S    12:18   0:00 sudo python /home/pi/tikinook/nook_controller.py
root       486 11.7  3.7 110344 35912 ?        Sl   12:18   0:04 python /home/pi/tikinook/nook_controller.py
pi        1205  0.0  0.2   4280  1948 pts/0    S+   12:19   0:00 grep --color=auto python

nook_controller.py is definitely the right Python script, and I'm guessing the /bin/sh means it's being run from a shell script… but I am still at a loss of where that shell script lives. Any ideas what my next steps would be to track this down?

2

I think I just figured it out. Thanks to my colleague Aaron Bendickson for the idea to check the systemd configuration file, which sparked the idea to check the status command below.

After the steps in the original post, next I tried systemctl status and that gave me this:

● tikinook3
    State: degraded
     Jobs: 0 queued
   Failed: 1 units
    Since: Wed 1969-12-31 16:00:03 PST; 48 years 7 months ago
   CGroup: /
           ├─1 /sbin/init
           ├─system.slice
           │ ├─...
           │ ├─cron.service
           │ │ ├─381 /usr/sbin/cron -f
           │ │ ├─402 /usr/sbin/CRON -f
           │ │ ├─452 /bin/sh -c sudo python /home/pi/tikinook/nook_controller.py
           │ │ ├─453 sudo python /home/pi/tikinook/nook_controller.py
           │ │ └─486 python /home/pi/tikinook/nook_controller.py
           ...

Ok, so it is in cron. But I already looked there.

$ crontab -e

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

The entire thing is commented out. So it's not in this crontab file. Is there another?

YES! — use sudo:

$ sudo crontab -e

# everything in the above file
# ...
@reboot sudo python /home/pi/tikinook/nook_controller.py

Whew!

That line should now be:

@reboot sudo python3 /home/pi/tikinook/nook_controller.py &

I am kind of wondering what that State: degraded means in the systemctl status results, but that's a question for another time. EDIT: I ran rpi-update and that degraded status resolved itself.

1

One way to discover who launched a particular process is to ask ps or pstree to print a process tree, revealing process parents:

ps -aef
pstree

However, this won't help if the parent process disowned your script or died.

Another idea is to find your script by name using grep:

grep -r "script name" /etc /var /usr/etc /home/pi/.config

The above commands should be run as root to make sure all processes and files are included in the search.

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.