我在脚本中有错误,但只有当它作为cron作业运行时才会出错

时间:2015-02-26 13:30:58

标签: bash variables cron

我有一个每小时运行一次的脚本,以便利用openvpn进行端口转发。从CLI运行时一切正常,但在通过相同的用户cron运行时失败。失败的部分是使用$ PORT值的结尾。

您可以看到值PORT和VPN_IP没有返回值,并且deluge命令失败。

这是直接运行的结果:

Your VPN ipaddress is  10.107.1.6
Contacting PIA for port forwarding .......
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   106  100    14  100    92      4     30  0:00:03  0:00:03 --:--:--    30
Port forwarding is currently using port 37186
Changing port settings on deluge....
Setting random_port to False..
Configuration value successfully updated.
Setting listen_ports to (37186, 37186)..

这里是通过cron运行的相同脚本 crontab中:     34 * * * * bash /home/alleyoopster/scripts/pia_portforward.sh> /home/alleyoopster/pia_port.log 2>& 1

没有返回VPN地址或端口地址且错误的结果:

Your VPN ipaddress is                                                                                                                 
Contacting PIA for port forwarding .......                                                                                            
Port forwarding is currently using port                                                                                               
Changing port settings on deluge....                                                                                                  
Setting random_port to False..                                                                                                        
Configuration value successfully updated.                                                                                             
malformed expression (,)                                                                                                              
Traceback (most recent call last):                                                                                                    
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/main.py", line 344, in do_command                                          
    ret = self._commands[cmd].handle(*args, **options.__dict__)                                                                       
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 104, in handle                                   
    return self._set_config(*args, **options)                                                                                         
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 140, in _set_config                              
    val = simple_eval(options["set"][1] + " " .join(args))                                                                            
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 87, in simple_eval                               
    res = atom(src.next, src.next())                                                                                                  
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 56, in atom                                      
    out.append(atom(next, token))                                                                                                     
  File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 79, in atom                                      
    raise SyntaxError("malformed expression (%s)" % token[1])                                                                         
SyntaxError: malformed expression (,)                




#! /bin/sh
#Simple bash script to facilitate Port Forwarding use with openvpn and PIA 
#Use as a cron job to run every hour
#This script will also change the port in deluge. It needs deluge-console installed
#Transmission should also work with the correct commands

#YOUR SETTINGS
#Private Internet Access Username and Password here
USERNAME="username"
PASSWORD="password"
#Enter the correct tun here. Normally tun0. The command ifconfig will list your network config
TUN="tun0"

#Get the local ip address 
VPN_IP=$(ifconfig $TUN|grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+"|tr -d "a-z :")
echo "Your VPN ipaddress is " $VPN_IP
echo Contacting PIA for port forwarding .......
TMP_PORT=$(curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat ~/.pia_client_id)&local_ip=$VPN_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment)

PORT=$(echo $TMP_PORT | sed "s/[^0-9]*//g")
echo "Port forwarding is currently using port "$PORT
echo "Changing port settings on deluge...."
deluge-console "config --set random_port False"
deluge-console "config --set listen_ports ($PORT,$PORT)"

1 个答案:

答案 0 :(得分:2)

听起来您的cron作业中的PATH设置与您的用户的PATH不匹配,而cron可能找不到ifconfig命令以便它可以获取VPN IP地址。

指定/sbin/ifconfig的完整路径以获取本地IP地址,或在crontab顶部添加以下行(我只是列出标准路径 - 根据需要进行调整以适应您的设置):

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