NMCLI works in script when I run itdirectly , but not when run in CRON
The reason while nmcli does not through cron is because the VPN details (username, password) reside in the account details of the user in the keyring. And nmcli requires DBus to retrieve the information.
That is, if you want to configure your system so that nmcli works, you need to get nmcli to work with the environment that CRON offers to the scripts.
See some examples at https://stackoverflow.com/questions/5819667/help-me-setup-a-cron-job-on-ubuntu and also at http://ubuntuforums.org/showpost.php?p=9016250&postcount=2 and https://wiki.archlinux.org/index.php/NetworkManager#Automatically_unlock_keyring_after_login
Just had a similar issue, and none of the above answers worked for me. I ended up simulating the problem with screen:
* * * * * /usr/bin/screen -dm
Add above line to cron, let it run once, turn it back off. Connect to your screen session (screen -r). If you are checking the screen session has been created (with ps
) be aware that they are sometimes in capitals (e.g. ps | grep SCREEN
)
Now nmcli
and similar will fail.
In particular, I noted that my problem was that nmcli dev wifi
would execute just fine, but nmcli nm wifi on
wouldn't.
The solution for me was quite simple in the end. I added a sudo
. Only necessary in cron. Not sure why this fixed it.