bash script that calls nmcli fails when called by crontab

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
newguard

bash script that calls nmcli fails when called by crontab

Post by newguard »

Hi all. I have a bash script that I can call from the command line that works perfectly. I also have crontab (same user) call it every 5 minutes to make sure the network is up and connected. It fails most of the time, but works once in a while. This is very perplexing, and I have become frustrated trying to tweak it in order to get it working so I don't have to babysit the machine. I wrote the script because I have a usb wireless dongle that intermittently disconnects. (Please don't address the dongle issue, it's been death by a thousand cuts already and it still won't stop disconnecting.) This script would be a fine solution if it would work from crontab (automatically) as it does from the command line. I know the script passes through both legs of the if-else loop, because the logfile has the right entries. It just seems that when called from crontab, it acts like the nmcli commands aren't there at all. Here is the code:

Code: Select all

#!/bin/bash
#check wifi status for 'connected'
wifistate=$(nmcli nm status)
set $wifistate

if [ $8 = "connected" ]
then
echo $i "`date` : Interface up." >> /home/user/logfiles/wlan1check.log
else
echo `nmcli -t nm wifi off` >/dev/null
echo `nmcli -t nm wifi on` >/dev/null
wait 3
echo $i "`date` : Interface reset." >> /home/user/logfiles/wlan1check.log
fi

tail -1000 /home/user/logfiles/wlan1check.log > /home/user/logfiles/wlan1check.logtemp
mv /home/user/logfiles/wlan1check.logtemp /home/user/logfiles/wlan1check.log

exit 0
Anyone have any ideas why this script fails when crontab calls it, but succeeds when I call it from the command line?
Could it be some kind of permission issue between crontab and the network manager?

Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: bash script that calls nmcli fails when called by crontab

Post by Pilosopong Tasyo »

Add, at the appropriate place in your script, the snippet of code from the following thread: viewtopic.php?f=213&t=80823#p470422

It should aide you in troubleshooting what happens during those times the script doesn't work. Usually, a command encountered an error and error messages that are supposed to show up in an open terminal window won't display since cron scripts normally run headless.

Since the script runs every five minutes, the additional code is going to open a terminal window on your display everytime it runs. Inspect the output and see where it went wrong.

Also, it may help if you temporarily disable redirecting to /dev/null (take out the echo command while you're at it) just to see what shows up while the script run its course.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
newguard

Re: bash script that calls nmcli fails when called by crontab

Post by newguard »

Thanks for that. I'll modify the script and see what the output is. I have redirected the output from all the commands and variables into a log file and have figured out that nmcli commands don't work when called from cron. So, I think I've narrowed it down to an environment variable or a permissions issue with nmcli being called from cron. I did however find a workaround in placing a script within the network manager "event" directories so that when the status of the wifi changes the script is called to reconnect it automatically. It's not ideal though because even when I manually disable the wifi is quickly reconnects....lol. If I want to turn wifi off, I just pull the dongle.

Anyway, thanks for the help. In the meantime I'll keep working on solving the underlying hardware/driver/comm issue so I won't need the script fixes. If I come to any reasonable insights or conclusions I'll post them here. After I figured out some better search terms to use, I discovered that others have similar problems with cron and nmcli, though the several solutions some have suggested have not worked for me (that would include exporting env's, absolute paths, etc.)

If I solve the problem or gather some better intel on the problem I'll post them. Cheers.
lmuserx4849

Re: bash script that calls nmcli fails when called by crontab

Post by lmuserx4849 »

man 5 crontab - talks about the environment available to a cron job.

I wonder if you fully-qualified the command, /usr/bin/nmcli or hard-coded a PATH=/sbin:/bin:/usr/sbin:/usr/bin in your user crontab.

One way to check the crontab environment is to compare it to your shell environment using env at the command line and in your user crontab (* * * * * env > $HOME/env.output)
Habitual

Re: bash script that calls nmcli fails when called by crontab

Post by Habitual »

lacking any env sourcing or other manipulation, I'd use

Code: Select all

wifistate=$(/path/to/nmcli nm status)
since bash usually has a limited $PATH environment.
Locked

Return to “Scripts & Bash”