Tcl pid Command
last modified October 20, 2026
The Tcl pid command returns the process ID of the current process
or the process IDs of a command pipeline opened with open. Process
IDs are numbers assigned by the operating system to running processes; they are
unique only among the processes running at the same time.
Basic Definition
The pid command with no arguments returns the process ID of the
current Tcl process. With a channel argument, it returns a list of the IDs of
the processes in the pipeline that the channel is connected to. For a channel
that is not a pipeline, such as stdout, the result is an empty
string.
Syntax: pid ?channelId?. The channelId is optional and refers to
a command pipeline created with open. A background command started
with exec has no channel; exec returns its process IDs
instead.
Getting Current Process ID
This example shows the simplest usage of pid to get the current
process ID.
set current_pid [pid] puts "Current process ID: $current_pid"
This retrieves and prints the process ID of the running Tcl interpreter. The ID will be different each time the script runs.
Getting Process ID of a Pipeline
When working with command pipelines, pid can return the process ID
of the external command. The result is a list with one ID per process in the
pipeline; here there is only one.
set pipeline [open "|ls -l"] set ls_pid [pid $pipeline] puts "ls command PID: $ls_pid" set output [read $pipeline] close $pipeline
This creates a pipeline to run the ls -l command and gets its
process ID. The output is read before the pipeline is closed; closing it while
the command still has data to write would kill the command with a broken pipe
error. Always remember to close pipelines when done.
Comparing Process IDs
You can compare process IDs to determine if two commands run in the same process.
set pid1 [pid]
set pid2 [pid]
if {$pid1 == $pid2} {
puts "Both PIDs are the same (same process)"
} else {
puts "Different processes"
}
This demonstrates that multiple pid calls in the same process
return the same value. The comparison will always be true in this case.
Process IDs of Background Processes
When exec starts a background process, it returns the process ID,
which can then be used to control the child process.
set child_pid [exec sleep 10 &] puts "Child process PID: $child_pid" exec kill $child_pid
This runs sleep in the background. When exec ends with
& it does not wait for the command and returns its process ID
instead of a channel. The script then ends the process with kill.
Process ID in File Operations
Process IDs can be useful when working with temporary files to create unique names.
set tempfile "/tmp/temp_[pid].txt" set fd [open $tempfile w] puts $fd "Temporary data" close $fd puts "Created temporary file: $tempfile"
This creates a temporary file with the process ID in its name, so that two
running programs do not use the same file. The file is opened, written to, and
then closed. The name is easy to guess and a stale file from an earlier process
with the same ID may exist, so for real temporary files prefer
file tempfile.
Monitoring Process Completion
A pipeline opened with open can be used to wait for an external
process to complete.
set channel [open "|sleep 5"] set process_id [pid $channel] puts "Waiting for process $process_id to complete..." close $channel puts "Process completed"
This starts a sleep command, prints its process ID and waits for
it to finish. The close command blocks until the process completes,
so "Process completed" is printed after about five seconds.
Best Practices
- Uniqueness: Process IDs are unique only while the process runs; the system may reuse them later.
- Cleanup: Always close channels to avoid resource leaks.
- Portability: Process behavior may vary across platforms.
- Temporary files: Do not rely on the process ID for secure temporary file names; use
file tempfile. - Channels:
pidreturns an empty string for a channel that is not a pipeline, and raises an error for a channel that does not exist.
This tutorial covered the Tcl pid command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.