1

This question already has an answer here:

is there an application that can collect commands and run them successively? This can be helpful when I have several commands to run that shouldn't run at the same time. It would be interesting too, to have a little window where I can see the running and upcoming commands.

I have searching for “linux shell batch list”, but couldn't find anything.

marked as duplicate by Rui F Ribeiro, Jesse_b, roaima, jimmij, Prvt_Yadv Mar 30 at 16:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Wouldn't shell scripts help? – Atul Mar 30 at 13:30
5

Traditionally, in a multi-user environment, batch was used for this sort of thing. Imagine 3 people working on a particular shared machine. Each might want to compile their program, but they only want one compilation to run at a time, so that they can all edit without too much disruption. Each could issue their own command, and the system would process each one sequentially.

echo cc a.c |batch 
echo make projectUser2 |batch 
echo make projectBUser3 |batch 

Each build will run in sequence.

You need to install the at package to get the batch command.

sudo apt-get install at

But if you are a single user on your own machine, then you can just edit a file and put each command on it's own line. eg Suppose you create a file name fileWithCommands with the following contents:

cc a.c
make projectUser2
make projectBUser3

Get bash to use it,

bash ./fileWithCommands

The other answers use more of the bash shell command language, to provide more intelligence reporting

If you want to save the output in a file then

bash ./fileWithCommands >ouput.log` 

Will do that, errors will be reported to the terminal. To get errors redirected to the same file correctly then the command is:

bash ./fileWithCommands >outputWithErrors.log 2>&1
2

It sounds like you are looking for a shell script which are fairly easy to learn but require more knowledge than could reasonably fit into a single answer.

I recommend reading up on:

The shell manual
The bash manual

These will have virtually all the information you need to begin shell scripting. Additionally the site you are on now is a great resource for shell scripting. As you go through this learning please ask as many questions as needed but try to keep them focused on a specific problem so they are easier to answer. However most questions you may have are likely already answered so you may be able to use the search function to find the answer you are looking for.

As for an answer more tailored to your specific question I have come up with the following function that would give you the ability to monitor commands as they are run:

run_command () {
    local command=($@)
    printf '%s' "Running: ${command[*]}..."
    if out=$("${command[@]}" 2>&1); then
        printf '%s\n' 'Successful'
        printf '%s\n' "$out"
    else
        printf '%s\n' 'Failed'
        printf '%s\n' "$out"
    fi
}

You would put this into a bash script and then feed it commands like so:

#!/bin/bash

run_command () {
    local command=($@)
    printf '%s' "Running: ${command[*]}..."
    if out=$("${command[@]}" 2>&1); then
        printf '%s\n' 'Successful'
        printf '%s\n' "$out"
    else
        printf '%s\n' 'Failed'
        printf '%s\n' "$out"
    fi
}

run_command sleep 1
run_command sleep 2
run_command ls -ltr ~/tmp
run_command ls -ltr /fake/dir

It would produce an output similar to:

$ ./script.sh
Running: sleep 1...Successful

Running: sleep 2...Successful

Running: ls -ltr /Users/jessebutryn/tmp...Successful
total 8
-rwxr-xr-x  1 jessebutryn  staff  337 Mar 30 07:41 script.sh
Running: ls -ltr /fake/dir...Failed
ls: /fake/dir: No such file or directory
2

Linux shells and CRON service provide a straight-forward way to accomplish this; Here we write a simple BASH script to get root partition used % and write to a log, then 10 seconds later, check Real Free Memory available and APPEND to same log file. We will use CRON to run this job every 5 minutes.

The script: run_commands.sh

[user ~]# cat run_commands.sh
   #!/bin/bash
   LOG=/home/user/command.log
   DISK=`df -h| grep -w / | awk '{print $5}'`
   echo `date -u` "" "Root PART % Used" = $DISK  >> $LOG
   sleep 10
   REALMEM=`free -m | awk 'NR==2{print $4}'`
   echo `date -u` "" "Real FREE Memory in MB" = $REALMEM  >> $LOG
   exit

Now lets have CRON run this script every 5 minutes:

crontab -e
*/5 * * * * /home/user/run_commands.sh
 wq:

Now lets see if its working by running a tail -f on the log file command.log

[user ~]# tail -f command.log
Sat Mar 30 13:40:01 UTC 2019  Root PART % Used = 11%
Sat Mar 30 13:40:11 UTC 2019  Real FREE Memory in MB = 482
Sat Mar 30 13:45:01 UTC 2019  Root PART % Used = 11%
Sat Mar 30 13:45:11 UTC 2019  Real FREE Memory in MB = 482

Shell scripting and CRON are tools I use almost daily. This script is very basic in that there are no args verification nor error checking. But its simple and gets the job done.

-Mark

  • 1
    Nice, I edited your example a little,(removed the ip). Could you edit your example with no need of being root and also check the crontab call / output? – el-teedee Mar 30 at 14:18
  • Sure, just a free AWS machine I use for dev.. – ZarNix Mar 30 at 14:20
  • think I got it now el-teedee. Thanks four your edits/advice – ZarNix Mar 30 at 14:29
0

The simple answer is, yes a shell script. Here is the basics

Create a file containing:

#!/bin/bash
command1
command2
command3
etc

Then make it executable.

chmod +X «the-file-name»

If you want to see a trace of what is happening. Then add set -x as the 2nd line (after the #!/bin/bash (this must be the 1st line), and before the commands that you want to trace).

Run the script by typing ./«the-file-name» (you need the ./ or other location info e.g. «another-directory-name»/«the-file-name»).

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