-1

I have a program that talks to a ttyACM0 usb device. When the USB bus is power-cycled the program is supposed to re-initialize the device handle.

  1. The program executes properly and re-initializes the Device-handle on USB power-cycle when called from the command-line

  2. Program executes properly if rc.local script is called after its called at-least once from the command line during a given session.

  3. If I boot the system rc.local executes but once the USB power-cycle starts the process is getting killed by the OS (atexit() printfs are not logged)

I have tried compiling the code over a fresh raspbian strech install and the problem still persists.

Can somebody throw some light behind this issue? I have been stuck on this issue for a week now. Any help is appreciated.

This is the rc.local file

#!/bin/sh -e
#
# rc.local
exec >>/data/rc_log.txt 2>&1
/root/bin/proc &
sleep  2
exit 0

The program gets sensor data from the ttyACM device and put it in a variable in a shared memory for another process to access and I need to it do so continuously, hence if the usb is removed, the program goes to sleep and once the usb is reconnected the program gets access again to the device and resumes

The program works properly when called from the command line using ./proc and it pauses and resumes when usb is removed or reattached. But when I reboot the rpi and make the program run from rc.local it works only till the device is connected and it exits once its disconnected from usb

The program is started and I can see it on htop, also my other process is getting sensor data from it. Once the usb device is removed the program dissappears from htop and since my ExitHandler is not being called as I cannot see any output in log I am assuming that the program is getting killed by the kernel.

Ingo
  • 42,107
  • 20
  • 85
  • 197
Balaji G
  • 1
  • 1
  • Please include /etc/rc.local and explain in more detail what the program is doing. – goldilocks Dec 16 '17 at 13:13
  • This is my rc.local file exec >>/data/rc_log.txt 2>&1

    /root/bin/proc & sleep 2

    exit 0 The program gets sensor data from the ttyACM device and put it in a variable in a shared memory for another process to access and I need to it do so continuously, hence if the usb is removed, the program goes to sleep and once the usb is reconnected the program gets access again to the device and resumes

    – Balaji G Dec 16 '17 at 13:44
  • The program works properly when called from the command line using ./proc and it pauses and resumes when usb is removed or reattached. But when I reboot the rpi and make the program run from rc.local it works only till the device is connected and it exits once its disconnected from usb – Balaji G Dec 16 '17 at 13:51
  • Edit details into the question, do not use comments. Until this resembles something that does not beg to waste other people's time the way mine was wasted because you could not be bothered to include the relevant line from /etc/rc.local, it will remain closed. Understand this is not a discussion forum wherein you can expect people to prod you for information in an ongoing dialog. Instead, you are expected to make an effort to present the problem in a complete form. Please take the tour to understand better how the site works. – goldilocks Dec 16 '17 at 14:07
  • 2
    If you have not yet, you should add signal and exit handlers into the program with logging, and all logged messages should be timestamped so you can present a concrete, detailed picture of when and how the process dies. – goldilocks Dec 16 '17 at 14:10
  • Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo Feb 22 '20 at 20:24

1 Answers1

0

A service started by init (which in the case of Raspbian and most other current GNU/Linux is systemd) is presumed to be a background process, unless special arrangements are made. A process which does not appropriately fork to the background within a small amount of time (seconds) will generally be killed by init.

What you describe is appropriate to a background process, but it sounds as if it is being run in the foreground like this:

/path/to/whatever/myprocess

Instead, if using /etc/rc.local you should fork it using POSIX shell syntax (&):

/path/to/whatever/myprocess &

exit 0

The exit 0 is there in the original script; it should remain the last line and you should ensure the script completes within a few seconds.

There may be complications if the process writes to standard output or error, in which case you should redirect that:

/path/to/whatever/myprocess > /dev/null 2>&1 &

This will throw the output away but you can use a logfile instead of /dev/null, see also:

Log output of background or boot script

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • Hi, Thanks for you tips. I am already forking the process with the "&" and redirecting the output and error streams to a log file using the syntax that you mentioned. – Balaji G Dec 16 '17 at 12:36
  • But the problem is I never get any output from this USB driver to the log since it is getting killed by the kernel (I get log information if I run the rc.local script by writing /etc/rc.local on the command line only after I run it by calling the process once by ./proc). Also the process running a while(1) loop to get data from the ttyACM device. I think the kernel decides to kill the process once the usb device is removed (the power cycle is part of the application though.) I am trying to find out why exactly the kernel kills the process. – Balaji G Dec 16 '17 at 12:43
  • I have checked dmesg and all the files in /var/log and I am unable to find anything. I used grep -i 'killed process' /var/log/xxx to search. – Balaji G Dec 16 '17 at 13:18
  • It sounds to me like you do not even know whether the program is started or not and your assertion that it is being killed is just a guess. – goldilocks Dec 16 '17 at 13:41
  • The program is started and I can see it on htop, also my other process is getting sensor data from it. Once the usb device is removed the program dissappears from htop. – Balaji G Dec 16 '17 at 14:51
  • Recent version of Raspbian and other GNU/Linux systems will not log much to a /var/log file unless you have explicitly configured them to do so. Instead you will have to use journalctl. You should also consider using a dedicated systemd service instead of rc.local since you can then query systemctl for status information that may provide a clue about why the process is being killed. – goldilocks Dec 16 '17 at 14:55
  • You might check systemctl status rc-local, since this is the service that runs that script. – goldilocks Dec 16 '17 at 14:59