Have 5 Scripts Running at Any Given Time

Assure only 1 instance of PowerShell Script is Running at any given Time

If the script was launched using the powershell.exe -File switch, you can detect all powershell instances that have the script name present in the process commandline property:

Get-WmiObject Win32_Process -Filter "Name='powershell.exe' AND CommandLine LIKE '%script.ps1%'"

Execute different nodejs scripts at a given time

I wouldn't create a node program just to schedule another node program. Use plain cron.

If you can run your job 16 times a day instead of 15 times, it would be every 90 minutes which you can schedule with these two cron expressions:

' 0 0/3 * * * node test.js'
'30 1/3 * * * node test.js'

If it has to be 15 times, this schedule has pretty good distribution (at most once an hour, at least every other hour, 15 times per day):

'0 0/2,1,9,17 * * * node test.js'

Any difficulty understanding this schedule, use this site. If you need your 15 invocations to be spaces equally (every 96 minutes), I'm afraid you'd need to break them into 5 schedules:

 0 0,8,16  * * * node test.js
36 1,9,17 * * * node test.js
12 3,11,19 * * * node test.js
48 4,12,20 * * * node test.js
24 6,14,22 * * * node test.js

Run multiple python scripts concurrently

With Bash:

python script1.py &
python script2.py &

That's the entire script. It will run the two Python scripts at the same time.

Python could do the same thing itself but it would take a lot more typing and is a bad choice for the problem at hand.

I think it's possible though that you are taking the wrong approach to solving your problem, and I'd like to hear what you're getting at.

How can I run multiple npm scripts in parallel?

Use a package called concurrently.

npm i concurrently --save-dev

Then setup your npm run dev task as so:

"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""

Shell fragment to make sure only one instance a shell script runs at any given time

Put this at the start of the script

SCRIPTNAME=`basename $0`
PIDFILE=/var/run/${SCRIPTNAME}.pid


if [ -f ${PIDFILE} ]; then
#verify if the process is actually still running under this pid
OLDPID=`cat ${PIDFILE}`
RESULT=`ps -ef | grep ${OLDPID} | grep ${SCRIPTNAME}`

if [ -n "${RESULT}" ]; then
echo "Script already running! Exiting"
exit 255
fi

fi


#grab pid of this process and update the pid file with it
PID=`ps -ef | grep ${SCRIPTNAME} | head -n1 | awk ' {print $2;} '`
echo ${PID} > ${PIDFILE}

and at the end

if [ -f ${PIDFILE} ]; then
rm ${PIDFILE}
fi

This first of all checks for the existence of the pid file and exits if it's present. If so then it confirms that a process under this script name with the old pid is running and exits if so. If not then it carries on and updates the script with the new pid file. The bit at the end checks for the existence of the pid file and deletes it, so the script can run next time.

Check permissions on /var/run are OK for your script though, otherwise create the PID file in another directory. Same directory as the script runs in would be fine.

How do you run multiple programs in parallel from a bash script?

To run multiple programs in parallel:

prog1 &
prog2 &

If you need your script to wait for the programs to finish, you can add:

wait

at the point where you want the script to wait for them.



Related Topics



Leave a reply



Submit