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) &
Related
English is not my native language, please accept my apologies for any language issues.
I want to execute a script (bash / sh) through CRON, which will perform various maintenance actions, including backup. This script will execute other scripts, one for each function. And I want the entirety of what is printed to be saved in a separate file for each script executed.
The problem is that each of these other scripts executes commands like "duplicity", "certbot", "maldet", among others. The "ECHO" commands in each script are printed in the file, but the outputs of the "duplicity", "certbot" and "maldet" commands do not!
I want to avoid having to put "| tee --append" or another command on each line. But even doing this on each line, the "subscripts" do not save in the log file. That is, ideally in the parent script, you could specify in which file each script prints.
Does not work:
sudo bash /duplicityscript > /path/log
or
sudo bash /duplicityscript >> /path/log
sudo bash /duplicityscript | sudo tee –append /path/log > /dev/null
or
sudo bash /duplicityscript | sudo tee –append /path/log
Using exec (like this):
exec > >(tee -i /path/log)
sudo bash /duplicityscript
exec > >(tee -i /dev/null)`
Example:
./maincron:
sudo ./duplicityscript > /myduplicity.log
sudo ./maldetscript > /mymaldet.log
sudo ./certbotscript > /mycertbot.log
./duplicityscript:
echo "Exporting Mysql/MariaDB..."
{dump command}
echo "Exporting postgres..."
{dump command}
echo "Start duplicity data backup to server 1..."
{duplicity command}
echo "Start duplicity data backup to server 2..."
{duplicity command}
In the log file, this will print:
Exporting Mysql/MariaDB...
Exporting postgres...
Start duplicity data backup to server 1...
Start duplicity data backup to server 2...
In the example above, the "ECHO" commands in each script will be saved in the log file, but the output of the duplicity and dump commands will be printed on the screen and not on the log file.
I made a googlada, I even saw this topic, but I could not adapt it to my necessities.
There is no problem in that the output is also printed on the screen, as long as it is in its entirety, printed on the file.
try 2>&1 at the end of the line, it should help. Or run the script in sh -x mode to see what is causing the issue.
Hope this helps
I want to run 2 commands (command1 and command2) in the same line, where command1 starts a background process and command2 starts a frontground process.
I tried:
command1 & ; command2
But it says: "-bash: syntax error near unexpected token `;'"
How could I run the 2 commands in the same line?
Try this:
(command1 &); command2
The the syntax (command) is creating a "subshell". You can read here something about it.
; is not helping here. The control operator you need here is & after the first command (thanks Nick Russo in comments):
command1 & command2
From man bash:
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell. The shell does
not wait for the command to finish, and the return status is 0.
Commands separated by a ; are executed sequentially; the shell waits
for each command to terminate in turn. The return status is the exit
status of the last command executed.
Test
$ sleep 10 & echo "yes"
[2] 13368
yes
$
[1]- Done sleep 10
$
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
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
This question already has answers here:
How do you run multiple programs in parallel from a bash script?
(19 answers)
Closed 1 year ago.
how can I execute a shell command in the background from within a bash script, if the command is in a string?
For example:
#!/bin/bash
cmd="nohup mycommand";
other_cmd="nohup othercommand";
"$cmd &";
"$othercmd &";
this does not work -- how can I do this?
Leave off the quotes
$cmd &
$othercmd &
eg:
nicholas#nick-win7 /tmp
$ cat test
#!/bin/bash
cmd="ls -la"
$cmd &
nicholas#nick-win7 /tmp
$ ./test
nicholas#nick-win7 /tmp
$ total 6
drwxrwxrwt+ 1 nicholas root 0 2010-09-10 20:44 .
drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 ..
-rwxrwxrwx 1 nicholas None 35 2010-09-10 20:44 test
-rwxr-xr-x 1 nicholas None 41 2010-09-10 20:43 test~
Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:
cmd="google-chrome";
"${cmd}" &>/dev/null & disown;
&>/dev/null sets the command’s stdout and stderr to /dev/null instead of inheriting them from the parent process.
& makes the shell run the command in the background.
disown removes the “current” job, last one stopped or put in the background, from under the shell’s job control.
In some shells you can also use &! instead of & disown; they both have the same effect. Bash doesn’t support &!, though.
Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":
cmd="google-chrome";
eval "${cmd}" &>/dev/null & disown;
If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.
Here's a function for it:
#!/bin/bash
# Run a command in the background.
_evalBg() {
eval "$#" &>/dev/null & disown;
}
cmd="google-chrome";
_evalBg "${cmd}";
Also, see: Running bash commands in the background properly
This works because the it's a static variable.
You could do something much cooler like this:
filename="filename"
extension="txt"
for i in {1..20}; do
eval "filename${i}=${filename}${i}.${extension}"
touch filename${i}
echo "this rox" > filename${i}
done
This code will create 20 files and dynamically set 20 variables. Of course you could use an array, but I'm just showing you the feature :). Note that you can use the variables $filename1, $filename2, $filename3... because they were created with evaluate command. In this case I'm just creating files, but you could use to create dynamically arguments to the commands, and then execute in background.
For example you have a start program named run.sh to start it working at background do the following command line.
./run.sh &>/dev/null &