0

I've got a Python script that is run from rc.local when my Raspberry Pi boots up. The script will store the PID, using os.getpid() in a plain text file.

pid = str(os.getpid())
pidfile = open("garage.pid", "w")
pidfile.write(pid)
pidfile.close() 

I have a bit of PHP code in my index.php that reports the status of the script, by reading the text file and checking for the process.

<?php
    $fileContent = file_get_contents("garage.pid");
    $PsStatus = exec("ps -a -p $fileContent >/dev/null && echo \"True\" || echo \"False\"");
    if($PsStatus == "True"){
        echo "Running";
    }
    if($PsStatus == "False"){
        echo "Stopped";
    }
?>          

When I run the above Python script manually from the shell, it works fantastically. However, when run from /etc/rc.local on bootup, it does save a PID number, but looking at the running processes, it does not show up and the PHP script fails.

Example of line added to rc.local:

(sleep 10;python /path/to/script.py)&

I've tried this with using a -u root on the ps command and still no luck. Any thoughts here?

EDIT: For example, when I let the script run from rc.local on reboot, the file is written with a value of '4126'.

Running 'ps -u root' shows:

 PID TTY          TIME CMD
   1 ?        00:00:02 init
   2 ?        00:00:00 kthreadd
   3 ?        00:00:44 ksoftirqd/0
   5 ?        00:00:00 kworker/0:0H
   6 ?        00:00:00 kworker/u8:0
   7 ?        00:00:34 rcu_preempt
   8 ?        00:00:00 rcu_sched
   9 ?        00:00:00 rcu_bh
   10 ?        00:00:00 migration/0
   11 ?        00:00:00 migration/1
   12 ?        00:00:31 ksoftirqd/1
   14 ?        00:00:00 kworker/1:0H
   15 ?        00:00:00 migration/2
   16 ?        00:00:13 ksoftirqd/2
   18 ?        00:00:00 kworker/2:0H
   19 ?        00:00:00 migration/3
   20 ?        00:00:11 ksoftirqd/3
   22 ?        00:00:00 kworker/3:0H
   23 ?        00:00:00 khelper
   24 ?        00:00:00 kdevtmpfs
   25 ?        00:00:00 netns
   26 ?        00:00:00 perf
   27 ?        00:00:00 khungtaskd
   28 ?        00:00:00 writeback
   29 ?        00:00:00 crypto
   30 ?        00:00:00 bioset 
   31 ?        00:00:00 kblockd
   33 ?        00:00:00 rpciod
   34 ?        00:00:00 kswapd0
   35 ?        00:00:00 fsnotify_mark
   36 ?        00:00:00 nfsiod
   42 ?        00:00:00 kthrotld
   44 ?        00:00:00 VCHIQ-0
   45 ?        00:00:00 VCHIQr-0
   46 ?        00:00:00 VCHIQs-0
   47 ?        00:00:00 iscsi_eh
   48 ?        00:00:00 dwc_otg
   49 ?        00:00:00 DWC Notificatio
   51 ?        00:00:00 VCHIQka-0
   52 ?        00:00:00 SMIO
   53 ?        00:00:00 deferwq
   54 ?        00:00:01 kworker/u8:2
   55 ?        00:00:01 mmcqd/0
   56 ?        00:00:00 jbd2/mmcblk0p6-
   57 ?        00:00:00 ext4-rsv-conver
   58 ?        00:00:03 kworker/2:1
   74 ?        00:00:01 kworker/3:1
  141 ?        00:00:00 scsi_eh_0
  142 ?        00:00:00 scsi_tmf_0
  143 ?        00:00:00 usb-storage
  178 ?        00:00:00 udevd
  302 ?        00:00:00 udevd
  308 ?        00:00:00 udevd
  417 ?        00:00:00 kworker/0:1H
 1093 ?        00:00:00 kworker/2:1H
 1591 ?        00:00:01 ifplugd
 1593 ?        00:00:04 ifplugd
 1616 ?        00:00:04 ifplugd
 1619 ?        00:00:07 RTW_CMD_THREAD
 1625 ?        00:00:00 wpa_supplicant
 2025 ?        00:00:00 rsyslogd
 2132 ?        00:00:01 apache2
 2226 ?        00:00:00 cron
 2266 ?        00:00:00 ntpd
 2368 ?        04:14:37 python
 2370 ?        00:00:00 startpar
 2399 tty1     00:00:00 getty
 2400 tty2     00:00:00 getty
 2401 tty3     00:00:00 getty
 2402 tty4     00:00:00 getty
 2403 tty5     00:00:00 getty
 2404 tty6     00:00:00 getty
 2405 ?        00:00:00 getty
 2513 ?        00:00:00 kworker/1:1H
 2740 ?        00:00:00 dhclient
 2806 ?        00:00:00 sshd
 9871 ?        00:00:00 kworker/0:2
 9957 ?        00:00:00 sshd
26236 ?        00:00:01 kworker/3:0
26287 ?        00:00:02 kworker/0:0
26555 ?        00:00:00 kworker/2:2
31051 ?        00:00:00 kworker/1:1
31127 ?        00:00:01 kworker/1:2
Ben Parmeter
  • 49
  • 2
  • 11
  • You want to open the file in "wb" mode to write a binary pid. I don't know PHP but I wonder if its something you should write as a string instead. That is, `pidfile.write(str(pid))`. – tdelaney Mar 11 '16 at 01:58
  • I thought str(os.getpit()) would return a string. Does the getpid function work differently when run as root (in the rc.local script) vs. sudo'ing in bash? – Ben Parmeter Mar 11 '16 at 02:38
  • Oh geez, your right. I missed that. The problem may be that you are not using an absolute path. You have a different current working directory when running `rc.local` so the file will be written there. – tdelaney Mar 11 '16 at 02:41
  • Actually, the file is written just fine. For example, right now it's been written after the script has run on a reboot and has a value of 4126. But when I run 'ps -u root', I only see Python running once with a different PID: ' 2368 ? 04:14:37 python '. – Ben Parmeter Mar 11 '16 at 04:18

1 Answers1

0

I resolved my problem by using the crontab (@reboot) method to run the python script on reboot. This apparently avoids an issue with using rc.local and trying to use os.getpid in python. Anyway, all is working now and I am happy! Thanks for everyone's help.

Run 'crontab -e' and added the following:

@reboot cd /pathto/scripts && sudo python script.py &

Removed/commented out the code from rc.local.

Ben Parmeter
  • 49
  • 2
  • 11