I want to run a lot of python scripts and save their outputs. Because running one by one in terminal is too cumbersome. As suggested, I decide to use a shell script (.sh).
To simplify the problem, let's say I have two python scripts to run (print1.py and print2.py). Script print1.py outputs number 1 and script print2.py outputs number 2.
If not using the shell script (i.e. enter commands separately), I can get the desired result.
If not using the shell script, the commands I would use are
$ nohup python print1.py &> 1.txt &
$ nohup python print2.py &> 2.txt &
which redirect the output to 1.txt and 2.txt, respectively. So 1.txt would contain a number 1, and 2.txt would contain a number 2. It works well if I enter these two commands separately in the terminal. This is what I desired.
Using the shell scipt, I can't get what I want (the redirection doesn't seem to work properly).
However, if I use a shell script, namely command.sh:
#! /bin/sh
nohup python print1.py &> 1.txt &
nohup python print2.py &> 2.txt &
And I type
$ sh command.sh
to run command.sh. The output is not what I expected. 1.txt and 2.txt will be created, but they contain nothing. A nohup.out file will also be created, in which number 1 and 2 are included.
And the output shown on the terminal is
$ nohup: appending output to 'nohup.out'
nohup: appending output to 'nohup.out'
This answer suggests that with #! /bin/sh, some shells may not understand &>. So I also tried changing #! /bin/sh to #! /bin/bash. However, it's also the same.
Can I get the same result using a shell script, just as I enter the commands separately?
sh command.sh, you still haveshrunning the script. What happens withbash command.sh? Or if you make the script (with#!/bin/bash) executable and run./command.sh?nohup: ignoring inputline at the beginning of file1.txtand2.txt. This doesn't affect the output, right? BTW, if you add an answer, I will mark it as accepted.