Press enter key in a shell script and ignore nohup output - linux

I have a basic shell script that I made for deploying my packages...
I am facing 2 issues while executing the command
nohup /home/username/wildfly/wildfly-20.0.1.Final/bin/standalone.sh &
Its keeping me frozen and saying the below
nohup: redirecting stderr to stdout
If I run this command in a normal way, I should press ENTER and the process will keep running in background fine.
So, my question is there any way to simulate that ENTER key stroke?
I have also an scp command to copy a file to another server and it's required to enter password after executing it, is there a way to enter the password through myscript.sh and press ENTER key?
scp /home/username/myfile.war 0.0.0.0:/home/username/myfile.war
Thanks in advance :)

It shouldn't actually freeze you, just look confusing because it has the "nohup:" message after the prompt for your next command (and then you need to press enter to get a new prompt). But you should be able to avoid the message by redirecting stdout and stderr yourself:
nohup /home/username/wildfly/wildfly-20.0.1.Final/bin/standalone.sh >/dev/null 2>&1 &
This sends both stdout and stderr to /dev/null, so nohup doesn't have to deal with them.

Related

Linux stop a command while its loading in PUTTY/SSH

Let me start of by saying i just started learning linux.
I have a command that keeps processing/loading until i press CTRL+C to cancel/stop the command and remain in session.
What i want is to send that command, wait x amount of seconds and then close it programmatically in a single command (cause i can't enter new commands when its loading). Sorry if it sounds stupid, but that would solve my problem.
Would really appreciate if someone could've helped me out here, couldn't find anything on google that was working (mostly exiting the session while the command remains loading).
Use sleep command for terminate the process.
example:
echo helloword & sleep 1 --> time indicates in seconds.After executing the echo command it will wait for one second and then it terminate the echo process
use nohup. example:
nohup ./foobar.sh & < /dev/null > ./log 2>&1
exit
In foobar.sh you can have the following content
command you wish to execute
sleep <no of seconds>
or you can use "screen", you need to install "screen" in you host, then
> screen {your-cmd}
and press ctrl+A followed ctrl+D detach current (but your cmd still running) session .

How can I run a Linux command that still runs after I close my PuTTY SSH session?

I am connecting to my NAS over putty which is running linux on it.
I wanted to move a big directory from one location to another. Is it possible to keep the process running after I close the putty session ?
I am afraid that if I close putty the files will not be copied to the end ?
Start your task with 'nohup' and put it in the background with '&', e.g.:
$ nohup mv /here /there &
$ exit
and it should continue running.
I would suggest using screen for this.
Start a new screen,
screen -S <name of your screen>
and then you can perform your commands there, detach from the screen and re-attach to it at any time.
Detach by hitting the sequence
ctrl a d
and re-attach by typing
screen -r (or list the screens with screen -l).
Also have a look at Gnu screen survival guide.
You can run it as a background process as follows:
nohup mv source target &
However, you will not be able to interact with the process.
EDIT: nohup is also required to keep it running after the shell exits.
Using nohup would be best solution.
The following command in your terminal may help you out to run the script using nohup and redirect the output in your desired file.
General Syntax
nohup some_command &> nohup_log_file_name.out &
Example
nohup python script.py &> nohup_log_script.out &
So, if you use the above command, you will be able to find the outputs of the command in a log file named nohup_log_script.out

How to simply ignore output from a program when called as an external one in vim?

I can dump the output content from my external command in the main window, I can disable "Press ENTER or type command to continue" and simply store it in a register.
But how do I call an external command in vim (it can be any program, apt-get, etc) and simply avoid it creating a buffer window if an output? Simply IGNORE the output from a external command I ran? I just want to call the command from vim. The command starts a simple webserver (listening on port 8080) and I have to press ctrl+c to stop it and move away from the external command buffer.
I tried silent before !cmd, it works, but I would like to stop the process my external command created right after it was started.
EDIT: I changed my mind about the simple webserver. I another situations, just doing like the suggestion accepted answer it works.
Pipe output to /dev/null:
:!cmd &> /dev/null
Use silent as you mention to get rid of the Press ENTER or type command to continue:
:silent !cmd &> /dev/null
Read this page for more on hiding this message.

Having to hit enter with nohup

I have a shell command like this
sudo nohup coffee -c -w *.coffee &
disown $!
wait
but when I run the shell scrit it says nohup: appending output to 'nohup.out' and makes me hit enter.
How do I get around having to hit enter?
8 year old thread, but I found that none of these answers really solve the issue in the question.
The message nohup: ignoring input and appending output to 'nohup.out' is piped through stderr (AFAIK), so in order to silence that message, all you have to do is to redirect stderr to /dev/null, like so:
nohup mycommand 2> /dev/null
However, if you additionally want to run this process in the background with &, you will find that (for bash at least), there will be a single line output of the job number and PID (e.g. [1] 27184). To avoid this, run the entire command in a subshell, like so:
(nohup my command 2> /dev/null &)
But if you're using this in a script, the former solution is sufficient.
As far as I understand, you don't have to. The message is output to the console, but not added to your input buffer. Therefore you can just continue typing your commands in as if there were no message from nohup, the message will not interfere with your input.
Well, having to type not from the exact prompt position may be aesthetically not so pleasing.
You could also redirect the log manually:
sudo nohup coffee -c -w *.coffee > /tmp/coffee.log &
That way the message won't show up at all.
Ubuntu Linux 20.04: None of the answers above solved the problem for me: script blocks in any case, waiting for input
My Solution
[sudo] nohup `command` > nohub.log < enter.txt &
where enter.txt is a text file containing a single line separator.

Asynchronous shell commands

I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
You can just run the script in the background:
$ myscript &
Note that this is different from putting the & inside your script, which probably won't do what you want.
Everyone just forgot disown. So here is a summary:
& puts the job in the background.
Makes it block on attempting to read input, and
Makes the shell not wait for its completion.
disown removes the process from the shell's job control, but it still leaves it connected to the terminal.
One of the results is that the shell won't send it a SIGHUP(If the shell receives a SIGHUP, it also sends a SIGHUP to the process, which normally causes the process to terminate).
And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.
The process won't receive any sent SIGHUP.
Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
Usually used with &(as a background job).
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
You can combine this with backgrounding,
nohup cmd &
and get rid of the output,
nohup cmd > /dev/null 2>&1 &
you can also disown a command. type cmd, Ctrl-Z, bg, disown
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
bg
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)
screen -m -d $command$ starts the command in a detached session. You can use screen -r to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen.

Resources