Getting sudo and nohup to work together

后端 未结 9 1632
滥情空心
滥情空心 2021-02-02 06:39

Linux newbie here.

I have a perl script which takes two command line inputs. I tried to run it in the background but this is what I got:

[~user]$ nohup s         


        
相关标签:
9条回答
  • 2021-02-02 07:09

    This should work

    sudo -b -u userName ./myScript > logFile
    

    I am just curious to understand that can I send this logFile as a email after the ./myScript is successful running in background.

    0 讨论(0)
  • 2021-02-02 07:12

    You must use sudo first, nohup second.

    sudo nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
    
    0 讨论(0)
  • 2021-02-02 07:12

    I open an editor and typed these lines:

    #!/bin/bash
    sudo echo Starting ...
    sudo -b MyProcess
    

    (Where MyProcess is anything I want to run as superuser.)

    Then I save the file where I want it as MyShellScript.sh .

    Then change the file permissions to allow execution. Then run it in a terminal. the "-b" option tells sudo to run the process separately in the background, so the process keeps running after the terminal session dies.

    Worked for me in linux-mint.

    0 讨论(0)
  • 2021-02-02 07:15

    You can set it as your alias:

    sudo sh -c 'nohup openvpn /etc/openvpn/client.ovpn 2>&1 > /dev/null &'
    
    0 讨论(0)
  • 2021-02-02 07:16

    First of all, you should switch sudo and nohup. And then:

    if sudo echo Starting ...
    then
        sudo nohup <yourProcess> &
    fi
    

    The echo Starting ... can be replaced by any command that does not do much. I only use it as dummy command for the sudo.

    By this the sudo in the if-condition triggers the password-check. If it is ok then the sudo session is logged in and the second call will succeed, otherwise the if will fail and not execute the actual command.

    0 讨论(0)
  • 2021-02-02 07:18

    Try:

    xterm -e "sudo -b nohup php -S localhost:80 -t /media/malcolm/Workspace/sites &>/dev/null"
    

    When you close xterm, the PHP web server still alive.
    Don't put nohup before sudo or else the PHP web server will be killed after closing xterm.

    0 讨论(0)
提交回复
热议问题