Gideon
Gideon

Reputation: 1886

Initiate multiple, simultaneous continuously running scripts via terminal, that continue after terminal session ends

I have a number of tasks that need to run constantly on the server. As they loop (and therefore aren't meant to come to an end) I'd like to run them in parallel rather than sequentially.

To run one of these scripts I can e.g.

 php myscript1.php;

In order to run them in parallel I've been using screens. So I create a screen specifically for a script, run it in that screen, then detach create a new one and run the next one in that one.

screen -S script1
php myscript1.php;
ctrl-a d

repeat

This was fine when there were e.g. 5. But if I restart the server, or want to make changes to how the scripts run it's getting pretty annoying - and there are an increasing number of these small looping tasks running.

I know that any time in terminal I'm working too hard its because I don't know something, so how can I for example run

php myscript1.php;  php myscript2.php;  php myscript3.php;

So they run simultaneously, and silently (no output)?

Edit: Of note (clarifying thanks to @bossman's comment below) these tasks are of varying and elastic length, and I do not want overlapping instances of the same task running, but I want them to run continuously.

This means a fixed cron job every 10 minutes would not be ideal as some might take e.g. 15 minutes to complete, or 2 minutes. Hence I'm hoping to use loops with a fixed sleep between loops.

Upvotes: 0

Views: 98

Answers (1)

Gideon
Gideon

Reputation: 1886

So I figured this out shortly after posting the question, but figured I'd leave it up in case others found it useful.

Here is my approach (though please feel free to let me know if there's a better way.

First start a screen: this is a session within a session that runs even when you close terminal, so this deals with any issue of scripts ending when you leave.

screen -S IMMORTALSCRIPTS

Having named and attached to that screen, run the commands simultaneously using & to join them typing (in my case)

php script1.php & php script2.php & php script3.php

They will all run at once, even showing mixed output on this screen, but you can leave the screen via

ctrl-a d

And return to your regular command line session and continue with anything else, or log out. This suits me because I'd like to be able to later check back on the script output (make sure they're running as expected, not throwing errors) via

screen -r IMMORTALSCRIPTS

Then on the need to restart them all if I've stopped them or restarted the server I can jump into that screen, tap the up arrow for last command and run the call to all script simultaneously.

Upvotes: 1

Related Questions