2

I feel like I have tried everything, my search history is about 20 pages deep on google trying to fix this and nothing works. Basically I have a script that checks if my tun0 interface exists, and if it is missing, the computer will restart. Here's the code:

#!/bin/bash
. /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if echo `ifconfig` | grep -q "tun0"
then echo ""
else
    reboot
fi

The script itself runs fine if I invoke it at a terminal using its path:

/scripts/vpnrestart1.sh

I've added this at the top of crontab -e:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

My cron task looks like this:

* * * * * /scripts/vpnrestart1.sh

Permissions are all fine on the script itself and it can be executed without using sudo. As I mentioned, it runs from the terminal just fine and works as designed. But if I put it in cron and remove the tun0 interface, nothing ever happens.

18
  • 1
    How come the path you're running manually has a "1" in it?
    – depquid
    Aug 12, 2016 at 3:52
  • 3
    How do you know it doesn't run? Did you try outputting something to a file to know what actually happens? Are you sure reboot works when run from that cron? Why don't you use the init system to manage this VPN? Aug 12, 2016 at 4:16
  • 1
    surely /etc/profile, not /etc.profile?
    – bobah
    Aug 12, 2016 at 7:27
  • 1
    And you can do just ifconfig | grep without the echo (not that it helps with the cron, either)
    – ilkkachu
    Aug 12, 2016 at 7:41
  • 1
    @roaima I've found that with this VPN program it doesn't always come back if you restart the app after a crash. The cleanest solution I could come up with was to reboot the VM because it's set to autostart on boot. I can't seem to find the emails, and there's no Cron entries in /var/log.. I am logged in as myself and I'm not sudoing when I edit the crontab so I assume the jobs are being scheduled as me. But I am not sure the "reboot" command requires root because I don't sudo when I trigger the command manually from terminal either.
    – coltsfan95
    Aug 12, 2016 at 12:07

3 Answers 3

2

Have you ever try debugging your script to see what happens when executed by cron? Insert below code into your script:

# log stdout and stderr to two different files
exec >>/var/log/looog.log 2>>/var/log/looog.err.log

# ...and log every command we try to execute to stderr (aka looog.err.log)
set -x

Once you script is executed those two files will be generated and you will be able to see what is happening.

1

If running reboot directly as a non-root user works when you're logged in, it's because your system is configured to allow users who are physically logged in to reboot the system. This works via Polkit.

From a crontab, you aren't in a logged-in session. A cron job that reboots the system would have to run as root.

I don't think rebooting is the solution to create a network interface. Just call whatever you do normally to bring up the VPN. And you'll probably need to be root to do that (you need to be root to set up a network interface, so either vpnrestart1.sh must run as root, or it gets root permissions via a setuid program such as sudo for the critical parts, or it delegates to NetworkManager which may use Polkit to allow some user sessions to affect the system configuration).

1
  • well the thing is, the VPN is set to auto-start on boot. I wasn't sure how many processes the program starts and uses to run so that's why I didn't have it re open the processes. I just triggered a reboot because I know the program will start cleanly in that case
    – coltsfan95
    Aug 16, 2016 at 0:56
1

Use full path instead of relative path in your shell script. This solved my issue.

1
  • Cron ought to have this written in upper case, everywhere... Jun 25, 2021 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .