Simple bash script not working in cron

I have a very simple bash script that checks if a vpn symlink exists and then complains if it doesn’t and it works just fine when ran from the prompt but for some reason the VPN variable remains empty when the script runs from cron. (vpn is named dynamically so can’t hardcode it)
I ran a find / >log from the script and it did show the /sys/class/net path so it shouldn’t be a file/selinux permission thing, it’s just not setting the variable.
Also tried VPN=find between ` as opposed to $(find) but that made no difference.
Both worked from the command line though.

#!/bin/bash

VPN=$(/usr/bin/find /sys/class/net/ -mindepth 1 -maxdepth 1 -name *vpn*)
if ! [ -L "$VPN" ]  ; then
        echo "VPN down! $VPN" 
fi

How about if you put quotes around *vpn*? Without the quotes, shell tries to pattern match in the current directory. Some shells will pass the pattern match on as a string if there is no match, some won’t. You want find to do the pattern match, not the shell spawned by cron.

2 Likes