2

I need to run a script that will create a connection when modem manager detects a modem (ie. mmcli -L lists the modem). I currently have a udev rule set up to do this, but mmcli takes so long to register the modem that the script has already finished running.

I am hoping there is a way to do one of the following things:

  1. Delay the start of the script from udev
  2. Allow the script to run in the background or go to sleep until mmcli sees the modem (I have been trying to get this to work, but calls to the sleep function are skipped, and it won't let me run in a different thread)
  3. Run script automatically when mmcli recognizes the modem

Here is a snippet of my most recent attempt:

sleep 10
count=0
while [ count < 300 ]
do
    index=$(mmcli -L | grep Modem | head -n1 | awk '{print $1;}')
    let "count+=1"
done
port=$(mmcli -m $index | grep 'primary port' | grep -oP 'ttyACM[0-9]')
connection=$(nmcli c show | grep "modem${port: -1}")
# check if connection does not exist
if [ ! $connection ]; then
    echo 'adding new connection at ' date >> /home/nvidia/runlog.txt
    nmcli c add type gsm ifname "${port}" con-name "modem${port: -1}" apn testers.apn.com
fi
nmcli c up "modem${port: -1}"
dessert
  • 39,982
Austin
  • 241
  • How about testing for the modem to show up in the output of mmcli -L inside the script before the connection part? Something like: while :;do mmcli -L|grep -q modem_name&&break;sleep 1;done – Or how about using the -w option to “Monitor the state of a given modem”, one could parse its output and act on that. – dessert Apr 23 '19 at 19:57
  • I will try, but up to now nothing in the script has worked. Udev is supposed to be used for short scripts and every time I try to slow the script down it doesn't work. I have put sleep statements in and it just ignores them. I have tried to put really long loops in the code, but it just ignores them, somehow. – Austin Apr 23 '19 at 20:00
  • @dessert your suggestion had some success, it didn't skip over the loop. It did have the unexpected consequence of modem manager never recognizing the modem. I think this has to do with the rule never finishing since it waits for the modem to register. Udev may have blocked it from being seen while the script was run, I will be looking more into this. – Austin Apr 23 '19 at 20:17

1 Answers1

2

I figured it out.

Udev is meant for short tasks, slowing down the script prevented the rest of the system from recognizing the modem had been plugged in. According to the udev manual

Add a program to the list of programs to be executed for a specific device. This can only be used for very short running tasks. Running an event process for a long period of time may block all further events for this or a dependent device. Long running tasks need to be immediately detached from the event process itself. If the option RUN{fail_event_on_error} is specified, and the executed program returns non-zero, the event will be marked as failed for a possible later handling.

Just detatching the process doesn't work, instead I was able to use at. See this answer for examples on how to use at: Conveniently schedule a command to run later?

Austin
  • 241