但仅当它作为cron作业运行时

但仅当它作为cron作业运行时

本文介绍了我在脚本中有错误,但仅当它作为cron作业运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本每小时运行一次,以促进使用openvpn进行端口转发.从CLI运行时,所有方法都运行良好,但在通过相同用户cron运行时,则运行失败.失败的部分是它使用值$ PORT的末尾.

I have a script that runs every hour to facilitate port forwarding with openvpn. It all works well when run from CLI, but fails when run through the same users cron. The part that fails is the end where it uses the value $PORT.

您可以看到PORT和VPN_IP值未返回值,并且deluge命令失败.

You can see that the values PORT and VPN_IP are not returning a value and the deluge command is failing.

这是结果直接运行:

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

and here is the same script run through croncrontab: 34 * * * * bash /home/alleyoopster/scripts/pia_portforward.sh > /home/alleyoopster/pia_port.log 2>&1

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

Result with no VPN address or Port address returned and errors:

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地址.

It sounds like the PATH setting in your cron job doesn't match your user's PATH, and cron may not be finding the ifconfig command so that it can obtain the VPN IP address.

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

Either specify the full path to /sbin/ifconfig to get the local IP address, or add the following line at the top of your crontab (I'm just listing standard paths - adjust as necessary to suit your setup):

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

这篇关于我在脚本中有错误,但仅当它作为cron作业运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:55