Batch Script - Process in Linux
Last Updated :
05 Feb, 2023
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.
Similar Reads
Processes in Linux/Unix A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution. Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example, pwd when i
6 min read
How to Manage Process in Linux A process means a program in execution. It generally takes an input, processes it, and gives us the appropriate output. It involves controlling and monitoring all the running programs on the system. This includes managing how much system resources each process gets, deciding when processes can use t
5 min read
Batch Script - Input / Output In this article, we are going to learn how to take input from users using Batch Script. Taking User Input:@echo off echo Batch Script to take input. set /p input= Type any input echo Input is: %input% pauseExplanation :The first 'echo' command is used to print a string.In the next line, 'set /p' com
1 min read
Batch Script - Logging Batch Script is a file that consists of various commands which need to be sequentially executed. It is not like coding and is not frequently used, in order to communicate with the OS through a command prompt, the user can use Batch Script. The commands are known as Batch commands. Logging refers to
2 min read
Process Control Commands in Unix/Linux Process control commands in Unix are: bg - put suspended process into background fg - bring process into foreground jobs - list processes bg Command : bg is a process control command that resumes suspended process while keeping them running in the background. User can run a job in the background by
3 min read
How to List Running Processes in Linux | ps Command As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out differe
10 min read