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

I have errors in a script, but only when it runs as a cron job

我有一个脚本,每小时 运行s 以促进 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 定时任务表: 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)"

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

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

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