zoukankan      html  css  js  c++  java
  • linux修改运行中的脚本

    工作中经常会遇到,在执行一个脚本时后,觉得有些地方要补充,而这个脚本又已经运行了一段时间,如果从头再来很浪费时间,这时如果能修改这个运行中的脚本就方便多了。

    如:

    cat test.sh

    #!/bin/bash

    echo 1111

    sleep 60

    echo 2222

     

    当开始运行后,如果想修改第二个echo的输出值,怎么办?

    直接打开test.sh修改肯定是不行的,这就好比,一个运行中的脚本,你把文件删除了之前,脚本依然可以运行下去一样。这是因为,此时的脚本已经放在了内存里。所以要想修改脚本只能去内存里修改。linux很方便的可以看到内存里的文件(/proc目录下)。

    通过:

    [root@en tmp]# ps aux |grep test.sh
    root     27887  0.0  0.4   4492  1100 pts/0    S    10:20   0:00 sh test.sh

    找到进程的pid,然后查看pid所对应的文件描述符:

    [root@en tmp]# cd /proc/27887/fd
    [root@en fd]# ll
    total 0
    lrwx------ 1 root root 64 May  8 10:21 0 -> /dev/pts/0
    lrwx------ 1 root root 64 May  8 10:21 1 -> /dev/pts/0
    lrwx------ 1 root root 64 May  8 10:20 2 -> /dev/pts/0
    lr-x------ 1 root root 64 May  8 10:21 255 -> /var/tmp/test.sh

    这里的255对应的文件就是我们的脚本,在这里直接编辑255这个文件

    [root@en fd]# vi 255

    #!/bin/bash
    echo 111111
    sleep 60
    echo 333333

    修改后保存退出,此时就会执行修改后的脚本了。

     

    要注意一点,如果是直接修改原脚本文件会出现什么结果呢?

    当改完后,再看fd下的文件,会发现:

    [root@en fd]# ll
    total 0
    lrwx------ 1 root root 64 May  8 10:27 0 -> /dev/pts/0
    lrwx------ 1 root root 64 May  8 10:27 1 -> /dev/pts/0
    lrwx------ 1 root root 64 May  8 10:27 2 -> /dev/pts/0
    lr-x------ 1 root root 64 May  8 10:27 255 -> /var/tmp/test.sh~ (deleted)

    被标记了已经删除,系统会认为原来的脚本文件已经找不到了,所有修改后是不会执行的。

    如果是一个for循环就不会生效了,如:

    for i in 1 2 3 4;do

    echo $i

    sleep 60

    done

    类似这样的脚本,执行中要加一些元素到循环中是做不到的,如想修改为:

    for i in 1 2 3 4 5 6 7;do

    echo $i

    sleep 60

    done

     

     

     

  • 相关阅读:
    opencvcircle圆
    关于“100g文件全是数组,取最大的100个数”解决方法汇总
    软件测试的核心价值
    写给测试新手
    软件测试职业规划
    关于软件测试职业规划的讨论
    云计算将改变传统软件测试行业
    对MVC模式的理解
    软件测试专家于涌谈行业前景与测试人员成长
    MVC模式
  • 原文地址:https://www.cnblogs.com/xuxyblog/p/3715617.html
Copyright © 2011-2022 走看看