3

I have a script that finally works, to automate all my calculations, but they take a while. The beginning of the script the user needs to input data, and then it runs till its done.

How can I keep my script running after the user input, but then in the same shell you can then carry on with other stuff

  • No time for a full answer, but look into the 'expect' command. Edit to clarify: this would input the data and then you would use & to put it into the background. – Kryštof Píštěk Jul 13 at 17:14
  • I would use screen, that can easily be detached. And re attached later on. – Archemar Jul 13 at 20:39
2

Add an ampersand (&) after your command within the shell script to run the script in the background ie:

#!/bin/bash
echo "type something"
read text
command $text &

The above is a rudimentary example of what I mean.

If you have already launched the script, Ctrl+Z will pause it and then bg would cause it to resume but in the background.

If whatever command you are running would output data to the standout, it will print it to the console even while running in the background. You will need to pipe the stdout for it not to do that. Typing fg will bring the backgrounded process to the foreground.

  • I clarified and provided an example to better show my suggestion to his solution. – am401 Jul 13 at 16:50
1

The simplest way to do this is to press Ctrl+Z (pause the script) after you've finished entering the input data and then bg which will send the script to the background and continue running it. You can then use your current shell session normally.

A better approach, however, is to modify your script so that it doesn't require any user input. Just pass whatever input you need as arguments when launching the script. This will make your life and the life of your users easier.

For example, instead of this:

#!/bin/bash
echo "Give me the first number! "
read num1

echo "Give me the second number! "
read num2

Use this:

#!/bin/bash
num1=$1
num2=$2

And run your script like this (where 10 and 30 are example numbers which will be saved as num1 and num2 respectively):

script.sh 10 30 > out 2>err&

That way, the script will be sent to the background immediately, any output will be redirected to the file called out and any error messages to the file called err. All parameters have been set up at launch time and no further interaction is needed.

As a general rule, avoid making scripts require user input whenever possible. Having to type in stuff to feed a script is laborious, error prone, much harder to automate and makes it harder to repeat the same operation later.

  • thank you for the input, the problem however is that there is quite extensive and important user input needed; its script to run software to run chemical calculations, so the input is very specific and crucial – Thomas Bates Jul 13 at 17:10
  • @ThomasBates that's even more reason to ensure you don't type it in! It is very easy to make a mistake that way. Either pass it as an argument or use a config file. Never write scripts that ask you to type in stuff, that's just asking for errors. And then there's no way of easily seeing what arguments you ran it with... Everything becomes a bit of a mess. – terdon Jul 13 at 17:13
0

Have you considered opening another shell window? I would be highly surprised if you'd be forced to use a VT-100 terminal or similar, those fell out of favor quite a while ago.

0

I suggest to use terminal multiplexers like tmux or screen for that. Using them you could run your program in background console and check it output from time to time or make notifcations in case of any activity on console with your program. Or even run it in separate pane and see your program output while doing other work in other pane.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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