How run PowerShell script all time? - windows

I have PowerShell script that is getting WorkingSet of one process.
When WorkingSet of process is higher then defined, it will kill the process. (process will start autmaticly - this is not part of my script)
How I can make that this script will run all-time? For example now when process will be using high RAM, script will immediately run itself and will kill this process?
Is this possible or is better to do some timer that will run for example for 10 seconds? 10 sec, run script, 10 sec, run script, ....?
And how to do that?
Thnak you for your help

You could put the script in a loop and run on startup (put it in the Startup folder)
while ($true)
{
Start-Sleep -s 10
# check process and kill if necessary
}
Or you could set up a Scheduled Task to run at midnight and repeat every minute for 24 hours, that would be a more efficient means but I don't think it gets finer than once a minute.

Related

Start multiple processes in Bash and time how long they take

How do I start multiple processes in bash and time how long they take?
From this question I know how to start multiple processes in a bash script but using time script.sh doesn't work because the processes spawned end after the script ends.
I tried using wait but that didn't change anything.
Here is the script in its entirety:
for i in `seq $1`
do
( ./client & )
done
wait # This doesn't seem to change anything
I'm trying to get the total time for all the processes to finish and not the time for each process.
Why the parentheses around client invocation? That's going to run the command in a subshell. Since the background job isn't in the top level shell, that's why the wait is ineffective (there's no jobs in this shell to wait for).
Then you can add time back inside the for loop and it should work.

Restart .bat file every 12 hours

i have a batch file on my server desktop called start.exe. I'd like to kill start.exe every 12 hours, than wait 2 seconds, and than restart start.exe. This should of course also work when no user is logged in on the server. So the workflow should be like:
Starting Windows Server
Starting start.exe
Restart every 12 hours
How can i achive this? With a script? With a task?
Run shutdown.exe in a scheduled task set to run every 12 hours. At a command prompt, type shutdown /? to see the options.

Delay in running a command in a shell script

I have written a shell script which includes some commands to collect various Test logs.But I want to put waiting time before a particular command so that it will run after 30 mins after starting the shell script.Anyone having the knowledge regarding it please help me out.
Thanks in advance
You can use the at command to schedule a job to run at any time:
echo "/my_path/my_script" | at now + 30 minutes
If you want a delay inside your script you can sleep to a specific time using this solution.

Run variable length bash script at the top of the hour without cron

I have a simple bash script that runs some tasks which can take varying amounts of time to complete (from 15 mins to 5 hours). The script loops using a for loop, so that I can run it an arbitrary number of times, normally back-to-back.
However, I have been requested to have each iteration of the script start at the top of the hour. Normally, I would use cron and kick it off that way, every hour, but since the runtime of the script is highly variable, that becomes trickier.
It is not allowable for multiple instances of the script to be running at once.
So, I'd like to include the logic to wait for 'top of the hour' within the script, but I'm not sure of the best way to do that, or if there's some way to (ab)use 'at' or something more elegant like that. Any ideas?
You can still use cron. Just make your script use a lock file. With the flock utility you can do:
#!/bin/bash
exec 42> /tmp/myscriptname.lock
flock -n 42 || { echo "Previous instance still running"; exit 1; }
rest of your script here
Now, simply schedule your job every hour in cron, and the new instance will simply exit if the old one's still running. There is no need to clean up any lock files.

Understanding the behavior of processes - why all process run together and sleep together?

I have written a script to initiate multi-processing
for i in `seq 1 $1`
do
/usr/bin/php index.php name&
done
wait
A cron run every min - myscript.sh 3 now three background process get initiated and after some time I see list of process via ps command. I see all the processes are together in "Sleep" or "Running" mode...Now I wanted to achieve that when one goes to sleep other processes must process..how can I achieve it?. Or this is normal.
This is normal. A program that can run will be given time by the operating system... when possible. If all three are sleeping, then the system is most likely busy and time is being given to other processes.

Resources