美文网首页
Linux 自启动.sh脚本使用sudo自动输入密码

Linux 自启动.sh脚本使用sudo自动输入密码

作者: 曼昱的小蓝毛巾 | 来源:发表于2021-07-01 15:06 被阅读0次

    1、root下,vi /etc/sudoers :

    [root@guanz etc]# vi /etc/sudoers
    [root@guanz etc]# 
    
    # 添加如下内容:
    guanz    ALL=(ALL)       ALL
    
    示例:
    ##
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    guanz    ALL=(ALL)       ALL
    ## Allows members of the 'sys' group to run networking, software,
    

    2、切换至普通用户guanz,并测试命令:

    2.1 方式一:【echo 'passwd' | sudo -S java -jar ****】

    [root@guanz etc]# su guanz
    [guanz@guanz etc]$ 
    
    # 进入jar包所在目录下:
    [guanz@guanz dell]$ cd app/
    [guanz@guanz app]$ ll
    total 599948
    -rw-rw-r--. 1 guanz guanz     59162 Jul  1 11:09 log-210701-01.txt
    -rwxrwxr-x. 1 guanz guanz 307142072 Jul  1 14:36 test-1.0.jar
    
    # 查看当前在运行的窗口程序
    [guanz@guanz app]$ screen -ls
    No Sockets found in /var/run/screen/S-guanz.
    
    # 创建窗口,并进入,直接执行java -jar启动,会提示你输入密码:
    [guanz@guanz app]$ screen -S jar
    [guanz@guanz app]$ sudo java -jar test-1.0.jar > log-01.txt
    [sudo] password for guanz: 
    
    # 通过【echo 'passwd' | sudo -S java -jar ****】再次执行,会直接启动,无需再输入密码。
    [guanz@guanz app]$ echo '123456' | sudo -S java -jar test-1.0.jar > log-01.txt
    [sudo] password for guanz: 
    
    
    # 查看当前在运行的窗口
    [guanz@guanz app]$ screen -ls
    There is a screen on:
        17362.jar   (Detached)
    1 Socket in /var/run/screen/S-guanz.
    [guanz@guanz app]$ 
    
    
    # 查看日志文件,发现文件已启动:
    2021-07-01 15:01:02.531  INFO 17548 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8890 (http) with context path ''
    2021-07-01 15:01:02.534  INFO 17548 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 29.065 seconds (JVM running for 30.46)
    
    至此,此命令有效。
    

    2.2 方式二:【EOF】文本块方式

    sudo -S java -jar test-1.0.jar > log-01.txt << EOF 
    > passwd
    > EOF
    
    # 通过【EOF】文本块的方式,再次执行,会直接启动,无需再输入密码。
    [guanz@guanz app]$ sudo -S java -jar test-1.0.jar > log-01.txt << EOF
    > 123456
    > EOF
    
    
    
    # 查看日志文件,发现文件已启动:
    2021-07-01 15:04:39.160  INFO 17848 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8890 (http) with context path ''
    2021-07-01 15:04:39.162  INFO 17848 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 29.111 seconds (JVM running for 30.618)
    
    
    至此,此命令有效。
    

    3、自启动.sh脚本文件:

    #!/bin/bash
    #cd ~/app
    #sudo java -jar test-1.0.jar > log.txt
    
    screen_name=$"jar"
    screen -dmS $screen_name
    cmd1=$"cd /home/app";
    screen -x -S $screen_name -p 0 -X stuff "$cmd1"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    cmd3=$"echo '123456' | sudo -S java -jar test-1.0.jar > log-01.txt";
    screen -x -S $screen_name -p 0 -X stuff "$cmd3"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    cmd4=$"screen -d jar";
    screen -x -S $screen_name -p 0 -X stuff "$cmd4"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    

    日志文件按照当天日期存储:

    #!/bin/bash
    #cd ~/app
    #sudo java -jar test-1.0.jar > log.txt
    currentdate=$(date +%Y%m%d)
    logdatefile="log-"$currentdate".txt"
    
    screen_name=$"jar"
    screen -dmS $screen_name
    cmd1=$"cd /home/app";
    screen -x -S $screen_name -p 0 -X stuff "$cmd1"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    cmd3=$"echo '123456' | sudo -S java -jar test-1.0.jar >  $logdatefile";
    screen -x -S $screen_name -p 0 -X stuff "$cmd3"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    cmd4=$"screen -d jar";
    screen -x -S $screen_name -p 0 -X stuff "$cmd4"
    screen -x -S $screen_name -p 0 -X stuff $'\n'
    

    再见。

    相关文章

      网友评论

          本文标题:Linux 自启动.sh脚本使用sudo自动输入密码

          本文链接:https://www.haomeiwen.com/subject/bmvjultx.html