Cannot get cronjob to work - scripts work when entred manually into shell
-
Hi all. I had made a previous topic earlier on the forum, explaining what I am doing:
http://forum.pfsense.org/index.php/topic,62598.0.htmlSo I resolved my first issue. The command was working perfectly fine, however my installation of Pfsense was corrupted and I was unaware of this. Reinstalling Pfsense fixed everything - curl commands work great now.
So basically, I have 3 scripts which are one line each, which I am trying to get working on a schedule in cron. I am only 14, and am not really sure where to get help with this. The commands that I enter into shell, do exactly as they are supposed to. The first two scripts simply pull my IP addresses while I am connected to my VPN. The third command, is to request the correct port that my VPN provider allows me to use (which changes randomly on their end)
Contents of the scripts
script1:
#!/bin/sh
ifconfig OPT1 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ipscript2:
#!/bin/sh
curl ifconfig.me/ip|tee /tmp/vpn_external_ipscript3:
curl -o /etc/portforwardlist -d "user=USERNAME&pass=PASSWORD&client_id=$(cat ~/.pia_client_id)&local_ip=$(cat /tmp/vpn_ip)" https://www.privateinternetaccess.com/vpninfo/port_forward_assignmentI have all of these scripts in /etc/
In "cron" I have configured the first script to run every minute, the second to run every 2 minutes, and the 3rd to run every 3 minutes. In script 3, i added the -o argument to output to a file. This does work when entered manally into shell as well.Cron Setup:
1 * * * * root /usr/bin/nice -n20 /etc/script1
2 * * * * root /usr/bin/nice -n20 /etc/script2
3 * * * * root /usr/bin/nice -n20 /etc/script3
I then reboot my system for good measure, however, none of the files get outputed. The commands work when entered manually into shell. I am so unsure where to go from here, and I hate asking, becuase I know BSD is for pro's. But I am so new to BSD at my age, and I love the features of Pfsense. I appologies if I sound very noobish, but just can't seem to get this to work.
Thanks for any assistance :)
-
use full path binaries on your script that is running via cron.
-
Use the full path to each executable - e.g.
/sbin/ifconfig /usr/bin/grep
and so on.
You can find where the utilities live with the find command, e.g.:
find / -name ifconfig /sbin/ifconfig
-
Okay thanks for the quick reply guys!
When you say the full path, are you refering to INSIDE the script? ex.
#!/bin/sh
/sbin/ifconfig OPT1 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ipinstead of:
#!/bin/sh
ifconfig OPT1 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ipThanks :D
-
-
Awesome. Thank you guys so much!
-
Okay so I just got rid of the last two, to make testing easier.
I have used the following as my script1.sh :
#!/bin/sh
/sbin/ifconfig ovpnc1 | /usr/bin/grep "inet " | /usr/bin/cut -d\ -f2|/usr/bin/tee /tmp/vpn_ipi have tried also putting in a space after the | in between /usr/bin/tee - with no avail. Any idea gents?
Thank you very much!
-
This works for me in cron:
#!/bin/sh /sbin/ifconfig ovpnc1 | /usr/bin/grep "inet" | /usr/bin/cut -f2 -d " " > /tmp/zzz.txt
I guess the "tee" utility does not work so well in a background job? Or maybe the "-d" did something odd?
-
Thanks very much Phil. I was starting to think that
maybe it was having difficulty with one of the commands.
I will give that a shot tonight when I get home from school.
And just verifying, the command you enter in CRON is just
to start the script, correct?ex.
1 * * * * root /usr/bin/nice -n20 /etc/script1OR do you put the actual shell command into the CRON entry?
(I dont think you do?). Sorry for all the newbish questions guys.Once I get this working, I am going to make a tutorial for getting
this setup entirely, as we have a lot of inexperienced users over
at my VPN providers boards trying to accomplish the same thing
I am. Thanks for all the help guys, you are too kind! -
think i may have found the error. on the tee line, i believe i need a " . "
/usr/bin/tee ./tmp/vpn_ip
-
/usr/bin/tee ./tmp/vpn_ip
why you need tee as it is running via cron?
did you tried to just redirect to your log file?
-
So I tried editing the script to how you said:
#!/bin/sh
/sbin/ifconfig ovpnc1 | /usr/bin/grep "inet" | /usr/bin/cut -f2 -d " " > /tmp/zzz.txtthis is a script - directory is /etc/script1.sh
the crontab entry looks as follows:
1 * * * * root /usr/bin/nice -n2 /etc/script1.sh
and still a no go. this is driving me crazy haha. I am on pfsense 2.1 beta if that
makes a huge difference?also, when i run the command in basic shell that Marcelloc posted, I end up getting two lines,
an ipv6 address and the ipv4 on line 2 -
I created /etc/script1.sh containing:
#!/bin/sh /sbin/ifconfig ovpnc1 | /usr/bin/grep "inet" | /usr/bin/cut -f2 -d " " > /tmp/zzz.txt
Then allow execute and everything on it (you can be more restrictive - this is for testing!):
chmod 777 /etc/script1.sh
Then add to cron:
*/1 * * * * root /usr/bin/nice -n20 /etc/script1.sh
"*/1" runs it every minute. Your setting of "1" in minutes will run it only at 1 minute past each hour.
On 2.1 there is IPv6. The OpenVPN gets an IPv6 "fe80:" address as well as IPv4. The output of /sbin/ifconfig contains lines for "inet" and "inet6", which both match your grep. The easy fix is to do
/usr/bin/grep -w "inet"
then it will match the word "inet" and not the word "inet6".
-
Thank you so much Phil for being patient with me.
I know it sucks answering super noobish stuff. I really do
appreciate it. I was totally unaware of the */1 as well,
so that is a big help. :D Will try again tonight, or maybe if
I can find an old PC in my computer class, I will give it a try
shortly. -
Alright!! Phil you were correct. That worked for the first script. The second one I managed to get working (somehow with the tee command). I ended up using:
#!/bin/sh
/usr/local/bin/curl ifconfig.me/ip|tee /tmp/vpn_externalNow I am on to the final command, I have tinkered with it for about 2 hours, and I can get it to output a file, but the file only contains {} , usually it would have the port inside the brackets, ex. {45000}. Currently I am using the following command (the X's represent my username and password. If I enter the command into shell, it does work) I tried using the full path to both cat commands (and without) but no go:
#!/bin/sh
/usr/local/bin/curl -o /tmp/portforwardlist -d "user=XXXXX&pass=XXXXX&client_id=$(/bin/cat ~/.pia_config)&local_ip=$(/bin/cat /tmp/vpn_ip)" https://www.privateinternetaccess.com/vpninfo/port_forward_assignmentALMOST THERE