Nohup not working with "." or ksh - ksh

I have a very unique issue. I have a *.b file, which I usually execute by putting a "." or the command ksh in front the name of the file. It usually works fine, but I tried to do the same using nohup and it won't work.
I put the following line:
nohup . file.b param1 param2 &
or
nohup ksh file.b param1 param2 &

Related

Bash: execute a string as if it was a script and pass parameters

If I have a bash script stored in a file I can execute it as follows:
./path-to-script.sh param1 param2 param3
now if I have that same script stored in a variable:
script="$(cat ./path-to-script.sh)"
How can I execute it with those same parameters?
(For context - I'm running the script on a remote computer, and have to copy paste it into the command to send. I could echo it into a file in the remote computer and execute that, but if I can avoid touching the file system that would be better).
Like this:
bash -c "$script" my_script param1 param2 param3
my_script is gonna be the name of your script and appear in error messages. For example:
$ bash -c 'echo ${1bad_var}' my_script
my_script: line 1: ${1bad_var}: bad substitution

How to multiple shell scripts passing with arguments

I have scripts a.sh and b.sh in which i have pass the ip as argument.
I tried running it as
sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &
But I see only first script runs.
When you run sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &:
sh -x a.sh 172.19.57.21 is one command, & sends it to background immediately
b.sh 172.19.57.21 is another command, again & puts it in background
The problem seems to me is that the script b.sh is not executable and as you are not running it as an argument to shell (unlike a.sh), it is failed in the PATH search.
You can run b.sh as shell's argument as well e.g:
sh a.sh 172.19.57.21 & sh b.sh 172.19.57.21 &
Or if both scripts are executables and have proper shebang:
./a.sh 172.19.57.21 & ./b.sh 172.19.57.21 &
I would recommend a wrapper to get the argument IP address once, and call required scripts from the wrapper, something like a tiny function would do:
wrapper() {
/path/to/a.sh "$#" &
/path/to/b.sh "$#" &
}
Now, you can just do e.g.:
wrapper 172.19.57.21
use ; between commands like
sh -x a.sh 172.19.57.21 &; sh -x b.sh 172.19.57.21 &

nohup VERBOSE=1 perl script.pl

I have a perl script for which ENV variables can be set to direct specific outputs
e.g. $debug, $verbose, $develop etc
Usually I run these from the command line
$ VERBOSE=1 perl myperlscript.pl params
I now want to run them using nohup. Using the command line
$ nohup VERBOSE=1 perl myperlscript.pl params
is clearly not right, as the attempt to set ENV{VERBOSE} is interpreted as a param to nohup & I get the msg
nohup: failed to run command `VERBOSE=1': No such file or directory
What IS the correct syntax here? I'm trying to run this on a linux box.
Set the environment variable before calling nohup, and it will be preserved when nohup exec()s (replaces itself with) perl.
$ VERBOSE=1 nohup perl myscript.pl params ...
This is exactly what the env command is for:
$ env VAR1=VAL1 VAR2=VAL2 nohup perl myperlscript.pl params &
Try to combine all commands into shell script and run it like that:
nohup /path/to/script.sh
Or you could use export:
export VERBOSE=1
And then:
nohup perl myperlscript.pl params

How to include nohup inside a bash script?

I have a large script called mandacalc which I want to always run with the nohup command. If I call it from the command line as:
nohup mandacalc &
everything runs swiftly. But, if I try to include nohup inside my command, so I don't need to type it everytime I execute it, I get an error message.
So far I tried these options:
nohup (
command1
....
commandn
exit 0
)
and also:
nohup bash -c "
command1
....
commandn
exit 0
" # and also with single quotes.
So far I only get error messages complaining about the implementation of the nohup command, or about other quotes used inside the script.
cheers.
Try putting this at the beginning of your script:
#!/bin/bash
case "$1" in
-d|--daemon)
$0 < /dev/null &> /dev/null & disown
exit 0
;;
*)
;;
esac
# do stuff here
If you now start your script with --daemon as an argument, it will restart itself detached from your current shell.
You can still run your script "in the foreground" by starting it without this option.
Just put trap '' HUP on the beggining of your script.
Also if it creates child process someCommand& you will have to change them to nohup someCommand& to work properly... I have been researching this for a long time and only the combination of these two (the trap and nohup) works on my specific script where xterm closes too fast.
Create an alias of the same name in your bash (or preferred shell) startup file:
alias mandacalc="nohup mandacalc &"
Why don't you just make a script containing nohup ./original_script ?
There is a nice answer here: http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135
#!/bin/bash
### make sure that the script is called with `nohup nice ...`
if [ "$1" != "calling_myself" ]
then
# this script has *not* been called recursively by itself
datestamp=$(date +%F | tr -d -)
nohup_out=nohup-$datestamp.out
nohup nice "$0" "calling_myself" "$#" > $nohup_out &
sleep 1
tail -f $nohup_out
exit
else
# this script has been called recursively by itself
shift # remove the termination condition flag in $1
fi
### the rest of the script goes here
. . . . .
the best way to handle this is to use $()
nohup $( command1, command2 ...) &
nohup is expecting one command and in that way You're able to execute multiple commands with one nohup

How do I create shortcut commands in the Ubuntu terminal?

For example, instead of typing all 5 commands in my terminal:
command 1
command 2
command 3
command 4
command 5
I just want to enter one command that runs all 5 commands above:
command everything
Is that possible? What would I need to edit in Ubuntu to do so?
If you're running a bash shell, you can type
alias commandall='command1 ; command2 ; command3 ; command4; command5'
Then commandall will run these commands
You can put the alias in your ~/.bashrc file and it will be there whenever you log in.
create a bash script.
#!/bin/bash
command1
command2
command3
then set its mode to executable
chmod a+x commandall
then you can call it from the command line
./commandall
if you put it in a directory in your PATH, you can call it like any other command.
~/bin
Write a shell script, mark it executable, put in in your path and then run it?
Alternatively you might write a shell function, put it in your ~/.bashrc.
You are probably looking for bash aliases -- try starting here :)
If you want every command be executed in the order, command1 && command2 && command3 && command4 && command5 should do. You could save it in a shell script and call the script when you need.
If the order of execution is not so important,
command1 &
command2 &
command3 &
command4 &
command5 &
should do.
You could as well mix and match if you need some other order of execution.
According to this:
$ (stsrun -v devel area1.exp; stsrun -v devel prime1.exp; stsrun -v devel treat.exp) &

Resources