- 1 year ago
problem with getting correct pid in tcsh shell
In the tcsh
shell, you can encounter issues when trying to retrieve the correct process ID (PID) due to the way tcsh
handles its internal job control. Here are a few points to consider:
tcsh
Backgrounding and Job Control:tcsh
uses a job control system where backgrounded processes are assigned job IDs (JIDs) instead of individual PIDs. The$$
variable intcsh
refers to the shell's PID, not the specific process running in the background.Use
$!
for Background Process PID: To get the PID of the most recently backgrounded process, you can use the$!
variable intcsh
. It contains the PID of the last background process created in the shell. For example:tcshcommand & # Run the command in the background
set pid = $! # Get the PID of the background process
Tracking Background Process PIDs: If you need to track multiple background processes and their PIDs, you may need to implement your own mechanism. For example, you can store the PIDs in an array or a file for later reference. Here's an example using an array:
tcshset bg_pids = () # Initialize an empty array for storing PIDs
command1 & # Run command1 in the background
set pid1 = $! # Get the PID of command1
set bg_pids = ($bg_pids $pid1) # Store PID in the array
command2 & # Run command2 in the background
set pid2 = $! # Get the PID of command2
set bg_pids = ($bg_pids $pid2) # Store PID in the array
# Access the PIDs from the array
echo $bg_pids[1] # Retrieve the first PID
echo $bg_pids[2] # Retrieve the second PID
Remember that tcsh
is not as widely used as other shells like Bash, and it has certain limitations and quirks. If you require more advanced process management and PID handling, you may consider using a different shell that provides more robust features in this area.