hitsword 发表于 2016-5-17 16:12:15

shell脚本请教

[*]#!/bin/bash
[*]PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;
[*]rm -rf /root/testfile/*.*
[*]cd /root/testfile/
[*]date >> /root/test.txt
[*]/usr/bin/lftp -u root,password sftp://123.123.123.123 -e "pget -n 30 /root/ubuntu-16.04-server-amd64.template;exit" >> /root/test.txt复制代码

上面代码保存成/root/test.sh
直接执行能够在test.txt写入:
2016年 05月 17日 星期二 15:50:55 CST
113242320 bytes transferred in 22 seconds (5.02M/s)

但是用crontab执行的话
*/5 * * * * sh /root/test.sh > /dev/null 2>&1

test.txt里只有“2016年 05月 17日 星期二 15:50:55 CST”,没有“113242320 bytes transferred in 22 seconds (5.02M/s)”。

看路由器流量图是有流量的

quyiqu 发表于 2016-5-17 16:31:40

#将.sh文件设为可执行(777)
chmod +x test.sh

hitsword 发表于 2016-5-17 16:48:29


优秀的苦瓜 发表于 2016-5-17 16:48

> /dev/null 2>&1

这个意思
去掉一样不行。感觉是lftp问题。

quyiqu 发表于 2016-5-17 16:56:00


hitsword 发表于 2016-5-17 16:56

去掉一样不行。感觉是lftp问题。
你用root权限执行的吗?

walkman660 发表于 2016-5-17 16:48:00

/usr/bin/lftp -u root,password sftp://123.123.123.123 -e "pget -n 30 /root/ubuntu-16.04-server-amd64.template;exit" >> /dev/null 2>&1

hitsword 发表于 2016-5-17 16:57:42


quyiqu 发表于 2016-5-17 16:31

#将.sh文件设为可执行(777)
chmod +x test.sh
已经是777.脚本内容是执行了的。

hitsword 发表于 2016-5-17 16:56:00


walkman660 发表于 2016-5-17 16:59

/usr/bin/lftp -u root,password sftp://123.123.123.123 -e "pget -n 30 /root/ubuntu-16.04-server-amd64 ...
我是要每次执行都记录。

leoynx 发表于 2016-5-17 16:59:41

去掉脚本里面重定向到test.txt,在cron里面重定向[*]*/5 * * * * /root/test.sh &> /root/test.txt复制代码

hitswordcgs3238 发表于 2016-5-17 17:14:22


leoynx 发表于 2016-5-17 17:23

去掉脚本里面重定向到test.txt,在cron里面重定向
还是不行。把pget的路径写全试下吧

hitsword 发表于 2016-5-17 16:31:00


cgs3238 发表于 2016-5-17 22:11

把pget的路径写全试下吧
pget不是程序。已经是完整路径。pget是lftp里面的一个参数
页: [1] 2
查看完整版本: shell脚本请教