Bala Krishnan
Bala Krishnan

Reputation: 374

How to run a script in background in Linux. - shell scripting

A script that keeps updating the log file. data like system time and date, users currently logged in etc for every interval of time say 5 minutes. THE SCRIPT MUST RUN EVEN AFTER THE TERMINAL HAS BEEN CLOSED.

Upvotes: 0

Views: 894

Answers (2)

Bala Krishnan
Bala Krishnan

Reputation: 374

Hurrah!! I would like to answer my question since i have got the solution.

For example, I'm running a script newscript.sh I want to run this in background and continue someother job in the terminal or i can close the terminal.

[yourname @ username ~]$ sh newscript.sh &

and hit enter. You will get a PID and your job will be attached to the background.

To kill the same process, use the PID

For eg., kill 1205212

Thank you.

Upvotes: 0

hgiesel
hgiesel

Reputation: 5648

Actually, no. First of, you don't need sh:

$ ./newscript.sh &

This is enough. This will start a background process. But your terminal is still controlling it. To achieve the behavior you want, do this:

$ disown %1

This will disown the job with the jobspec 1 (which is like an id), which was the one you started beforehand. Now you can close the terminal.

Upvotes: 1

Related Questions