Can I change the name of `nohup.out`? - bash

When I run nohup some_command &, the output goes to nohup.out; man nohup says to look at info nohup which in turn says:
If standard output is a terminal, the
command's standard output is appended
to the file 'nohup.out'; if that
cannot be written to, it is appended
to the file '$HOME/nohup.out'; and if
that cannot be written to, the command
is not run.
But if I already have one command using nohup with output going to /nohup.out and I want to run another, nohup command, can I redirect the output to nohup2.out?

nohup some_command &> nohup2.out &
and voila.
Older syntax for Bash version < 4:
nohup some_command > nohup2.out 2>&1 &

For some reason, the above answer did not work for me; I did not return to the command prompt after running it as I expected with the trailing &. Instead, I simply tried with
nohup some_command > nohup2.out&
and it works just as I want it to. Leaving this here in case someone else is in the same situation. Running Bash 4.3.8 for reference.

Above methods will remove your output file data whenever you run above nohup command.
To Append output in user defined file you can use >> in nohup command.
nohup your_command >> filename.out &
This command will append all output in your file without removing old data.

As the file handlers points to i-nodes (which are stored independently from file names) on Linux/Unix systems You can rename the default nohup.out to any other filename any time after starting nohup something&. So also one could do the following:
$ nohup something&
$ mv nohup.out nohup2.out
$ nohup something2&
Now something adds lines to nohup2.out and something2 to nohup.out.

my start.sh file:
#/bin/bash
nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &
There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.
I <3 forever

Related

Logs are not inserting in 'nohup.out' file

I am executing script1.sh through crontab.
script1.sh
echo "Crontab is executing this script."
echo "Some code is running here."
cd cron
./script2.sh
'script1.sh' is invoking 'script2.sh'.
'script2.sh'
echo "Exceuting script2."
echo "log of script3.sh is inserting in nohup.out file."
nohup sh script3.sh &
echo "Above syntax is not logging in nohup.out file. but this syntax is 'sh script3.sh > nohup.out &' is logging in nohup.out file. Why is it so?"
'script2.sh' is invoking 'script3.sh',but not able to log in nohup.out file. For logging following syntax is used.
nohup sh script3.sh &
'script3.sh' contains below mention line of codes.
echo "Executing script3. This is just test example to simulate the things."
Why logs are not inserting in nohup.out file?
From info nohup:
23.4 ‘nohup’: Run a command immune to hangups
=============================================
‘nohup’ runs the given COMMAND with hangup signals ignored, so that
the command can continue running in the background after you log out.
...
If standard output is a terminal, the command’s standard output is
appended to the file ‘nohup.out’;
if that cannot be written to, [waltera: the write permission/space in current dir] it is
appended to the file ‘$HOME/nohup.out’; and if that cannot be written
to, the command is not run.
...
However, if standard output is closed, standard error terminal output
is instead appended to the file ‘nohup.out’ or ‘$HOME/nohup.out’ as
above.
When cron executes the script, stdout is not a terminal nor closed, so nohup has no reason to redirect any output.
Another demo of nohup without a terminal is when you start your script2.sh, that has an nohup command, with another nohup:
nohup ./script2.sh > masternohup.out
The output of script3.sh will be written to masternohup.out.
From man nohup
...
If standard input is a terminal, redirect it from an unreadable file.
If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use 'nohup COMMAND > FILE'.
...
So it could be in $HOME/nohup.out either way it's best to use this to control output:
nohup COMMAND &> FILE
You don't need nohup in modern days.
Shell escape method allows a process to leave its process group and never receive SIGHUP nor any other signals directed to a process group.
In bash shell:
(command &>log.txt &)

nohup command could not generate the nohup.out file by rundeck job

My rundeck job execute a simple nohup command "cd /tmp/; nohup ls -l &". the job execution log will get the command output but not generate the nohup.out file in that directory.
i used all kind of steps in rundeck workflow. all couldnot generate the nohup.out file.
Do anyone have any idea why rundeck couldnot generate the nohup.out file but send the command output to the execution logs?? thank
You didn't seem to redirect the output to a file, nohup works fine in RunDeck from my experience.
cd /tmp/; nohup ls -l > /tmp/nohup.something
I have a working theory: nohup is being consumed/interpreted by the Rundeck infrastructure and interpreted approximately as: 'run this job indefinitely in the background' without our usual understanding of 'with output to a non-tty' (i.e. both stdout/stderr to nohup.out or piped together to file). Or at least, I am fairly certain it is not executing the version of nohup that is on my path.
Thus:
you should manually redirect all of the output (stdout and stderr) to file unless you are content to let Rundeck capture it
you do not need the &, although it does not hurt anything
In my case, I translated from:
cd tools/celery && nohup ./start.sh flower > log_name.txt &
to:
cd tools/celery && nohup ./start.sh flower > log_name.txt 2>&1

How to completely hide nohup messages?

I try to completely hide nohup messages from my terminal. My nohup usage looks like this:
if eval 'nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt >& /dev/null &'; then
but in console I get one nohup message in Polish:
nohup: zignorowanie wejścia i przekierowanie standardowego błędu do standardowego wyjścia
which means "ignoring standard input redirecting standard error to standard output"
Is there any chance to hide every nohup message?
The trick I use is to run nohup in a sub-shell and redirect the standard error output of the sub-shell to /dev/null:
if (nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt & ) 2>/dev/null
then : …whatever…
else : …never executed…
fi
However, the exit status of that command is 0, even if the grep fails to do anything (mine wrote grep: /tmp/dict.txt: No such file or directory in nohup.out, for example), but the exit status was still 0, success. The one disadvantage of the notation used here is that the job is run by the sub-shell, so the main process cannot wait for it to complete. You can work around that with:
(exec nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt ) 2>/dev/null &
This gives you the job control information at an interactive terminal; it won't in a non-interactive script, of course. You could also place the & outside the sub-shell and after the error redirection in the first command line, without the exec, and the result is essentially the same.

Run a bash script via another bash script to delete a file is not working properly

I have a bash script start.sh which calls another run.sh, which takes me to another prompt where I have to delete a file file.txt and then exit out of that prompt.
When I call run.sh from inside start.sh, I see the prompt and I believe that it deletes the file.txt but the inner/new prompt waits for me to exit out of it while the script is running - meaning it needs intervention to proceed. How do I avoid it in bash?
In Python I can use Popen and get it going but not sure about bash.
EDIT: I would rather like to know what command to provide to exit out of the shell (generated from running run.sh") so I can go back to the prompt where "start.sh" was started.
Etan: To answer your question
VirtualBox:~/Desktop/ > ./start
company#4d6z74d:~$ ->this is the new shell
company#4d6z74d:~$ logout ---> I did a "Control D here" so the script could continue.
Relevant part of start.sh which:
/../../../../run.sh (this is the one that takes us to the new $ prompt)
echo "Delete file.txt "
rm -f abc/def/file.txt
You can run run.sh in the background using &. In start.sh, you would invoke the script via /path/run.sh &. Now, start.sh will exit without waiting for run.sh to finish (which is running in the background).

Simply forking and redirecting the output of a command to /dev/null

I frequently execute from a shell (in my case Bash) commands that I want to fork immediately and whose output I want to ignore. So frequently in fact that I created a script (silent) to do it:
#!/bin/bash
$# &> /dev/null &
I can then run, e.g.
silent inkscape myfile.svg
and my terminal will not be polluted by the debug output of the process I just forked.
I have two questions:
Is there an "official" way of doing this?, i.e. something shorter but equivalent to &> /dev/null & ?
If not, is there a way I can make tab-completion work after my silent command as if it weren't there ? To give an example, after I've typed silent inksc, I'd like bash to auto-complete my command to silent inkscape when I press [tab].
aside: probably want to exec "$#" &> /dev/null & in your silent script, to cause it to discard the sub-shell, and the quotes around "$#" will keep spaces from getting in the way.
As for #2: complete -F _command silent should do something like what you want. (I call my version of that script launch and have complete -F launch in my .bash_profile)
It looks like nohup does more or less what you want. The tab-completion problem is because bash thinks that you are trying to complete a filename as an argument to the script, whereas its completion rules know that nohup takes a command as its first argument.
Nohup redirects stout and stderr to nohup.out and will also leave the command running if your shell exits.
Here's a little script I use for launching interactive (and chatty) X apps from e.g. an xterm
#!/bin/bash
exe="$1"
shift
"$exe" "$#" 2>/tmp/$$."$exe".err 1>&2 & disown $!
No output, won't die if the terminal exits, but in case something goes wrong there's a log of all output in /tmp
If you don't want the log just use /dev/null instead.
Also will work from a function if you're script-alergic.
Perhaps if you could 'rebind' the tab key? An example on superuser Stackoverflow with the enter key is shown. Is this the right idea?

Resources