1

I want to run a script with nohup because it takes long time but it needs a password to run. how can I write the command using nohup. For example to run the script interactively I first run php hat and when it asks for a key or password I write 123456.

I tried nohup php hat & password (for example 123) but that didn't work.

2

When you run a command with nohup, the stdin is redirected from /dev/null. So you cannot use nohup and manually enter a password.

Depending up on the program that you're using, you can use redirections to enter the password:

$ nohup bash -c 'read; echo $REPLY' <<<'hello'
$ tail -1 nohup.out 
hello

So you could save your password in a file (say password.txt), and encrypt it using something like gpg with a password-protected private key (to get password.txt.gpg), and do:

gpg -d password.txt.gpg | nohup php hat

Or, you could use something like screen.

First start a screen session (this command actually either starts a session named my-php-script-session, or brings one that is already running to the foreground):

screen -D -RR -S my-php-script-session

Then in the shell that opens, run your command normally:

php hat

Enter your password, and then detach the screen session: Ctrla, followed by d.

You can re-attach it by running the first screen command again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.