Open In App

Batch Script – Process in Linux

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Batch Script is basically a script having commands which execute sequentially. It helps users to perform repetitive tasks without human or user intervention or input. To automate some tasks like loading processes, and killing processes.

Now in this article, we will write a batch script to view all the processes that are running, once getting all the running processes, select the particular process and put it into a text file separately, and kill the process that you want according to your need 

Step 1:  Create a text file and save it using the “.bat” extension, and write the following batch commands.

Batch script for the process

@echo off 
TASKLIST
tasklist > process.txt
tasklist /fi "memusage gt 50000"
taskkill /f /im notepad.exe
pause

In the above script 

@echo off: this command prevents from displaying commands on prompt

TASKLIST: list out all the processes that are running

tasklist > process.txt:   this command takes the output of the TASKLIST command and saves it into process.txt file 

tasklist /fi “memusage gt 50000”:  This command fetches only those process which has memory size 50MB or greater

taskkill /f /im notepad.exe: this command kills the mentioned task. to kill a given process that must be running in the background.

pause: this command is used to pause the currently running script.

Step 2: Run the batch.bat file 

When the TASKLIST command runs it will produce the following output : 

list of processes

tasklist > process.txt  command output: all the processes pushed into process.txt file.

list of processes pushed into process.txt

tasklist /fi “memusage gt 50000”: this will fetch the process those having size 50MB or greater

processes having a size 50Mb or greater

taskkill /f /im notepad.exe: this will kill or close the notepad that is open in the background. 

if notepad is not open in the background then this command will produce the following output : 

notepad not open 

otherwise, if notepad is open then :

notepad open

This is how you can perform the sequence of tasks automatically using batch script.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads