加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell脚本帮助 – 接受输入并在BackGround中运行?

发布时间:2020-12-15 21:51:00 所属栏目:安全 来源:网络整理
导读:我有一个 shell脚本,在第一行中我要求用户输入他们希望脚本运行的分钟数: #!/usr/bin/kshecho "How long do you want the script to run for in minutes?:c"read scriptdurationloopcnt=0interval=1date2=$(date +%H:%M%S)(( intervalsec = $interval * 1
我有一个 shell脚本,在第一行中我要求用户输入他们希望脚本运行的分钟数:

#!/usr/bin/ksh
echo "How long do you want the script to run for in minutes?:c"
read scriptduration
loopcnt=0
interval=1
date2=$(date +%H:%M%S)
(( intervalsec = $interval * 1 ))
totalmin=${1:-$scriptduration}
(( loopmax = ${totalmin} * 60 ))

ofile=/home2/s499929/test.log
echo "$date2 total runtime is $totalmin minutes at 2 sec intervals"
while(( $loopmax > $loopcnt ))
do
  date1=$(date +%H:%M:%S)
   pid=`/usr/local/bin/lsof | grep 16752 | grep LISTEN |awk '{print $2}'` > /dev/null 2>&1
   count=$(netstat -an|grep 16752|grep ESTABLISHED|wc -l| sed "s/ //g")
   process=$(ps -ef | grep $pid | wc -l | sed "s/ //g")
   port=$(netstat -an | grep 16752 | grep LISTEN | wc -l| sed "s/ //g")
  echo "$date1 activeTCPcount:$count activePID:$pid activePIDcount=$process listen=$port" >> ${ofile}
  sleep $intervalsec
  (( loopcnt = loopcnt + 1 ))
done

如果我手动输入数值,它的效果很好.但如果我想运行3个小时,我需要启动脚本以在后台运行.

我试过运行./scriptname&我明白了

$How long do you want the test to run for in minutes:360
ksh: 360:  not found.
[2] + Stopped (SIGTTIN)        ./test.sh &

脚本死了.这是可能的,关于我如何接受这一个输入然后在后台运行的任何建议?谢谢!!!

解决方法

你可以这样做:

test.sh arg1 arg2 &

只需在bash脚本中将arg1和arg2分别称为$1和$2即可. ($0是脚本的名称)

所以,

test.sh 360 &

将传递360作为bash或ksh脚本的第一个参数,在脚本中可以称为$1.

所以你的脚本的前几行现在是:

#!/usr/bin/ksh
scriptduration=$1
loopcnt=0
...
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读