Girish K Girish K - 4 months ago 18
Bash Question

Shell scripting - Is it possible to execute .sh file in background

I have one

.sh
script. After running this particular file. It is showing list of deleted files. I want to execute this file in the background instead of showing on the shell on which I have run this command.

Please advise, is there any way to achieve that? Thanks.

Answer

Just run it as:

./script.sh >/home/username/output.txt 2>&1 &

The & at the end sends the command to the background.

The >/home/username/output.txt 2>&1 sends standard error and standard output to output.txt.

To keep it running after you log out, change it to this:

nohup ./script.sh >/home/username/output.txt 2>&1 &