How to use ps command to show list of processes from current user only in Linux
Assistant
In Linux, you can use the ps command to display a list of processes for the current user. The command you want is:
bash
ps -u $(whoami)
Explanation:
ps: This is the command that shows the current processes.
-u: This option is used to specify the user. It allows you to filter the processes by a particular user.
$(whoami): This command substitution returns the username of the currently logged-in user, and ps -u $(whoami) shows processes owned by that user.
Alternatively, you can also use:
bash
ps aux | grep $(whoami)
Explanation:
User
How to freeze and unfreeze process by name in Linux?
Assistant
In Linux, you can freeze and unfreeze processes using the kill command with specific signals. To freeze (pause) a process, you can send it the SIGSTOP signal, and to unfreeze (resume) it, you can send it the SIGCONT signal. Below are the steps to freeze and unfreeze a process by its name.
Step 1: Find the Process ID (PID)
First, you need to get the PID of the process you wish to freeze. You can use the pgrep command for this:
bash
pgrep <process_name>
Replace <process_name> with the actual name of the process. This will return the PID(s) of the process(es) with that name.
ps aux: This command displays all processes for all users.
|: This is a pipe, which takes the output of the command on the left and uses it as input for the command on the right.
grep $(whoami): This filters the output to show only those lines that contain the current user's username.
However, the first method (ps -u $(whoami)) is more straightforward and efficient for just listing processes of the current user.
Example Output:
When you run ps -u $(whoami), you might see output similar to this:
code
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
john 12345 1.0 0.5 123456 67890 ? S 10:00 0:01 gnome-terminal
john 12346 0.0 0.1 123456 12345 ? S 10:00 0:00 python script.py
This output describes the processes along with their Process ID (PID), CPU and memory usage, and the command that started each process.
Step 2: Freeze the Process
Once you have the PID(s), you can freeze the process using:
bash
kill -SIGSTOP <PID>
Or, if you want to do this for all processes with a specific name, you can use:
bash
pkill -SIGSTOP <process_name>
Step 3: Unfreeze the Process
To unfreeze the process, use the SIGCONT signal:
bash
kill -SIGCONT <PID>
Or to unfreeze all processes with that name:
bash
pkill -SIGCONT <process_name>
Example
Here is a practical example for a process named myapp.
Find the PID:
bash
pgrep myapp
Output might look like:
code
1234
Freeze the process:
bash
kill -SIGSTOP 1234
Or
bash
pkill -SIGSTOP myapp
Unfreeze the process:
bash
kill -SIGCONT 1234
Or
bash
pkill -SIGCONT myapp
Notes:
You might need superuser privileges (using sudo) depending on the ownership of the processes.
SIGSTOP cannot be caught or ignored by the process, which means it will stop immediately when you send this signal.
SIGCONT resumes the process, allowing it to continue execution.