date int64 1,220B 1,719B | question_description stringlengths 28 29.9k | accepted_answer stringlengths 12 26.4k | question_title stringlengths 14 159 |
|---|---|---|---|
1,387,949,048,000 |
I would like to monitor one process's memory / cpu usage in real time. Similar to top but targeted at only one process, preferably with a history graph of some sort.
|
On Linux, top actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
| How to monitor CPU/memory usage of a single process? |
1,387,949,048,000 |
I would like to avoid doing this by launching the process from a monitoring app.
|
On Linux with the ps from procps(-ng) (and most other systems since this is specified by POSIX):
ps -o etime= -p "$$"
Where $$ is the PID of the process you want to check. This will return the elapsed time in the format [[dd-]hh:]mm:ss.
Using -o etime tells ps that you just want the elapsed time field, and the = at ... | How to check how long a process has been running? |
1,387,949,048,000 |
What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.
|
There are 3 methods that I'm aware of:
pwdx
$ pwdx <PID>
lsof
$ lsof -p <PID> | grep cwd
/proc
$ readlink -e /proc/<PID>/cwd
Examples
Say we have this process.
$ pgrep nautilus
12136
Then if we use pwdx:
$ pwdx 12136
12136: /home/saml
Or you can use lsof:
$ lsof -p 12136 | grep cwd
nautilus 12136 saml cwd DIR... | Find out current working directory of a running process? |
1,387,949,048,000 |
In ps xf
26395 pts/78 Ss 0:00 \_ bash
27016 pts/78 Sl+ 0:04 | \_ unicorn_rails master -c config/unicorn.rb
27042 pts/78 Sl+ 0:00 | \_ unicorn_rails worker[0] -c config/unicorn.rb
In htop, it shows up like:
W... |
By default, htop lists each thread of a process separately, while ps doesn't. To turn off the display of threads, press H, or use the "Setup / Display options" menu, "Hide userland threads". This puts the following line in your ~/.htoprc or ~/.config/htop/htoprc (you can alternatively put it there manually):
hide_user... | Why does `htop` show more process than `ps` |
1,387,949,048,000 |
I get the message There are stopped jobs. when I try to exit a bash shell sometimes. Here is a reproducible scenario in python 2.x:
ctrl+c is handled by the interpreter as an exception.
ctrl+z 'stops' the process.
ctrl+d exits python for reals.
Here is some real-world terminal output:
example_user@example_server:~$ ... |
A stopped job is one that has been temporarily put into the background and is no longer running, but is still using resources (i.e. system memory). Because that job is not attached to the current terminal, it cannot produce output and is not receiving input from the user.
You can see jobs you have running using the jo... | There are stopped jobs (on bash exit) |
1,387,949,048,000 |
The Linux proc(5) man page tells me that /proc/$pid/mem “can be used to access the pages of a process's memory”. But a straightforward attempt to use it only gives me
$ cat /proc/$$/mem /proc/self/mem
cat: /proc/3065/mem: No such process
cat: /proc/self/mem: Input/output error
Why isn't cat able to print its own memo... |
/proc/$pid/maps
/proc/$pid/mem shows the contents of $pid's memory mapped the same way as in the process, i.e., the byte at offset x in the pseudo-file is the same as the byte at address x in the process. If an address is unmapped in the process, reading from the corresponding offset in the file returns EIO (Input/out... | How do I read from /proc/$pid/mem under Linux? |
1,387,949,048,000 |
I accidentally "stopped" my telnet process. Now I can neither "switch back" into it, nor can I kill it (it won't respond to kill 92929, where 92929 is the processid.)
So, my question is, if you have a stopped process on linux command line, how do you switch back into it, or kill it, without having to resort to kill -... |
The easiest way is to run fg to bring it to the foreground:
$ help fg
fg: fg [job_spec]
Move job to the foreground.
Place the job identified by JOB_SPEC in the foreground, making it the
current job. If JOB_SPEC is not present, the shell's notion of the
current job is used.
Exit Status:
Statu... | If you ^Z from a process, it gets "stopped". How do you switch back in? |
1,387,949,048,000 |
How does one find large files that have been deleted but are still open in an application? How can one remove such a file, even though a process has it open?
The situation is that we are running a process that is filling up a log file at a terrific rate. I know the reason, and I can fix it. Until then, I would like to... |
If you can't kill your application, you can truncate instead of deleting the log file to reclaim the space. If the file was not open in append mode (with O_APPEND), then the file will appear as big as before the next time the application writes to it (though with the leading part sparse and looking as if it contained ... | Find and remove large files that are open but have been deleted |
1,387,949,048,000 |
I have detached a process from my terminal, like this:
$ process &
That terminal is now long closed, but process is still running, and I want to send some commands to that process's stdin. Is that possible?
|
Yes, it is. First, create a pipe:
mkfifo /tmp/fifo.
Use gdb to attach to the process:
gdb -p PID
Then close stdin: call close (0); and open it again: call open ("/tmp/fifo", 0600)
Finally, write away (from a different terminal, as gdb will probably hang):
echo blah > /tmp/fifo
| How do I attach a terminal to a detached process? |
1,387,949,048,000 |
I want to see list of process created by specific user or group of user in Linux
Can I do it using ps command or is there any other command to achieve this?
|
To view only the processes owned by a specific user, use the following command:
top -U [username]
Replace the [username] with the required username
If you want to use ps then
ps -u [username]
OR
ps -ef | grep <username>
OR
ps -efl | grep <username>
for the extended listing
Check out the man ps page for options
An... | How to see process created by specific user in Unix/linux |
1,387,949,048,000 |
For Windows, I think Process Explorer shows you all the threads under a process.
Is there a similar command line utility for Linux that can show me details about all the threads a particular process is spawning?
I think I should have made myself more clear. I do not want to see the process hierarcy, but a list of all... |
The classical tool top shows processes by default but can be told to show threads with the H key press or -H command line option. There is also htop, which is similar to top but has scrolling and colors; it shows all threads by default (but this can be turned off). ps also has a few options to show threads, especially... | Is there a way to see details of all the threads that a process has in Linux? |
1,387,949,048,000 |
Sometimes, I would like to unmount a usb device with umount /run/media/theDrive, but I get a drive is busy error.
How do I find out which processes or programs are accessing the device?
|
Use lsof | grep /media/whatever to find out what is using the mount.
Also, consider umount -l (lazy umount) to prevent new processes from using the drive while you clean up.
| How do I find out which processes are preventing unmounting of a device? |
1,387,949,048,000 |
Are there any relatively strightforward options with top to track a specific process?
Ideally by identifying the process by a human readable value? e.g. chrome or java.
In other words, I want to view all the typical information top provides, but for the results to be filtered to the parameters provided i.e.. 'chrome' ... |
You can simply use grep:
NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
DESCRIPTION
grep searches the named input FILEs (or standard input if no files are named, or if a single
... | How to view a specific process in top |
1,387,949,048,000 |
In Unix whenever we want to create a new process, we fork the current process, creating a new child process which is exactly the same as the parent process; then we do an exec system call to replace all the data from the parent process with that for the new process.
Why do we create a copy of the parent process in th... |
The short answer is, fork is in Unix because it was easy to fit into the existing system at the time, and because a predecessor system at Berkeley had used the concept of forks.
From The Evolution of the Unix Time-sharing System (relevant text has been highlighted):
Process control in its modern form was designed and... | Why do we need to fork to create new processes? |
1,387,949,048,000 |
How can I get the command arguments or the whole command line from a running process using its process name?
For example this process:
# ps
PID USER TIME COMMAND
1452 root 0:00 /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/run/udhcpc.eth0.pid
And what I want is /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/r... |
You could use the -o switch to specify your output format:
$ ps -eo args
From the man page:
Command with all its arguments as a string. Modifications to the arguments may be shown. [...]
You may also use the -p switch to select a specific PID:
$ ps -p [PID] -o args
pidof may also be used to switch from process nam... | How to get whole command line from a process? |
1,387,949,048,000 |
Suppose I have a thousand or more instances of any process (for example, vi) running. How do I kill them all in one single shot/one line command/one command?
|
What's wrong with the good old,
for pid in $(ps -ef | grep "some search" | awk '{print $2}'); do kill -9 $pid; done
There are ways to make that more efficient,
for pid in $(ps -ef | awk '/some search/ {print $2}'); do kill -9 $pid; done
and other variations, but at the basic level, it's always worked for me.
| Kill many instances of a running process with one command |
1,387,949,048,000 |
I'm looking for somthing like top is to CPU usage. Is there a command line argument for top that does this? Currently, my memory is so full that even 'man top' fails with out of memory :)
|
From inside top you can try the following:
Press SHIFT+f
Press the Letter corresponding to %MEM
Press ENTER
You might also try:
$ ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5
This will give the top 5 processes by memory usage.
| How to find which processes are taking all the memory? |
1,387,949,048,000 |
I recently came across this in a shell script.
if ! kill -0 $(cat /path/to/file.pid); then
... do something ...
fi
What does kill -0 ... do?
|
This one is a little hard to glean but if you look in the following 2 man pages you'll see the following notes:
kill(1)
$ man 1 kill
...
If sig is 0, then no signal is sent, but error checking is still performed.
...
kill(2)
$ man 2 kill
...
If sig is 0, then no signal is sent, but error checking is still performed; ... | What does `kill -0` do? |
1,387,949,048,000 |
Given an X11 window ID, is there a way to find the ID of the process that created it?
Of course this isn't always possible, for example if the window came over a TCP connection. For that case I'd like the IP and port associated with the remote end.
The question was asked before on Stack Overflow, and a proposed method... |
Unless your X-server supports XResQueryClientIds from X-Resource v1.2 extension I know no easy way to reliably request process ID. There're other ways however.
If you just have a window in front of you and don't know its ID yet — it's easy to find it out. Just open a terminal next to the window in question, run xwinin... | What process created this X11 window? |
1,387,949,048,000 |
I'm writing an application. It has the ability to spawn various external processes. When the application closes, I want any processes it has spawned to be killed.
Sounds easy enough, right? Look up my PID, and recursively walk the process tree, killing everything in sight, bottom-up style.
Except that this doesn't wo... |
Update
This is one of those ones where I clearly should have read the question more carefully (though seemingly this is the case with most answers on to this question). I have left the original answer intact because it gives some good information, even though it clearly misses the point of the question.
Using SID
I th... | Kill all descendant processes [duplicate] |
1,387,949,048,000 |
Is it possible to block the (outgoing) network access of a single process?
|
With Linux 2.6.24+ (considered experimental until 2.6.29), you can use network namespaces for that. You need to have the 'network namespaces' enabled in your kernel (CONFIG_NET_NS=y) and util-linux with the unshare tool.
Then, starting a process without network access is as simple as:
unshare -n program ...
This crea... | Block network access of a process? |
1,387,949,048,000 |
I read here that the purpose of export in a shell is to make the variable available to sub-processes started from the shell.
However, I have also read here and here that "Processes inherit their environment from their parent (the process which started them)."
If this is the case, why do we need export? What am I miss... |
Your assumption is that shell variables are in the environment. This is incorrect. The export command is what defines a name to be in the environment at all. Thus:
a=1 b=2
export b
results in the current shell knowing that $a expands to 1 and $b to 2, but subprocesses will not know anything about a because it is not ... | If processes inherit the parent's environment, why do we need export? |
1,387,949,048,000 |
What are session leaders, as in ps -d which selects all processes except session leaders?
|
In Linux, every process has several IDs associated with it, including:
Process ID (PID)
This is an arbitrary number identifying the process. Every process has a unique ID, but after the process exits and the parent process has retrieved the exit status, the process ID is freed to be reused by a new process.
Parent ... | What are "session leaders" in `ps`? |
1,387,949,048,000 |
How can I verify whether a running process will catch a signal, or ignore it, or block it? Ideally I'd like to see a list of signals, or at least not have to actually send the signal to check.
|
Under Linux, you can find the PID of your process, then look at /proc/$PID/status. It contains lines describing which signals are blocked (SigBlk), ignored (SigIgn), or caught (SigCgt).
# cat /proc/1/status
...
SigBlk: 0000000000000000
SigIgn: fffffffe57f0d8fc
SigCgt: 00000000280b2603
...
The number to the right is ... | How can I check what signals a process is listening to? |
1,387,949,048,000 |
How to limit process to one cpu core ?
Something similar to ulimit or cpulimit would be nice. (Just to ensure: I do NOT want to limit percentage usage or time of execution. I want to force app (with all it's children, processes (threads)) to use one cpu core (or 'n' cpu cores)).
|
Under Linux, execute the sched_setaffinity system call. The affinity of a process is the set of processors on which it can run. There's a standard shell wrapper: taskset. For example, to pin a process to CPU #0 (you need to choose a specific CPU):
taskset -c 0 mycommand --option # start a command with the given affin... | How to limit a process to one CPU core in Linux? [duplicate] |
1,387,949,048,000 |
It often baffles me that, although I have been working professionally with computers for several decades and Linux for a decade, I actually treat most of the OS' functionality as a black box, not unlike magic.
Today I thought about the kill command, and while I use it multiple times per day (both in its "normal" and -... |
Sending kill -9 to a process doesn't require the process' cooperation (like handling a signal), it just kills it off.
You're presuming that because some signals can be caught and ignored they all involve cooperation. But as per man 2 signal, "the signals SIGKILL and SIGSTOP cannot be caught or ignored". SIGTERM ca... | How does Linux "kill" a process? |
1,387,949,048,000 |
I'm going through this book, Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel. It's from 2001, so a bit old. But I find it quite good anyhow.
However, I got to a point when it diverges from what my Linux produces in the shell output. On page 92 (116 in the viewer), the chapter 4.5 GNU/Linux... |
I think this part of the clone(2) man page may clear up the difference re. the PID:
CLONE_THREAD (since Linux 2.4.0-test8)
If CLONE_THREAD is set, the child is placed in the same thread
group as the calling process.
Thread groups were a feature added in Linux 2.4 to support the
... | Are threads implemented as processes on Linux? |
1,387,949,048,000 |
I know that pkill has more filtering rules than killall. My question is, what is the difference between:
pkill [signal] name
and
killall [signal] name
I've read that killall is more effective and kill all processes and subprocesses (and recursively) that match with name program. pkill doesn't do this too?
|
The pgrep and pkill utilities were introduced in Sun's Solaris 7 and, as g33klord noted, they take a pattern as argument which is matched against the names of running processes. While pgrep merely prints a list of matching processes, pkill will send the specified signal (or SIGTERM by default) to the processes. The co... | What's the difference between pkill and killall? |
1,387,949,048,000 |
Most shells provide functions like && and ; to chain the execution of commands in certain ways. But what if a command is already running, can I still somehow add another command to be executed depending on the result of the first one?
Say I ran
$ /bin/myprog
some output...
but I really wanted /bin/myprog && /usr/bin/... |
You should be able to do this in the same shell you're in with the wait command:
$ sleep 30 &
[1] 17440
$ wait 17440 && echo hi
...30 seconds later...
[1]+ Done sleep 30
hi
excerpt from Bash man page
wait [n ...]
Wait for each specified process and return its termination status. Each n
... | Can I somehow add a "&& prog2" to an already running prog1? |
1,387,949,048,000 |
I have been using this command successfully, which changes a variable in a config file and then executes a Python script within a loop:
for((i=114;i<=255;i+=1)); do echo $i > numbers.txt; python DoMyScript.py; done
As each DoMyScript.py instance takes about 30 seconds to run before terminating, I'd like to relegate t... |
Drop the ; after &. This is a syntactic requirement
for((i=114;i<=255;i+=1)); do echo $i > numbers.txt;python DoMyScript.py & done
| Use & (ampersand) in single line bash loop |
1,387,949,048,000 |
I want to run multiple commands (processes) on a single shell. All of them have own continuous output and don't stop. Running them in the background breaks Ctrl-C. I would like to run them as a single process (subshell, maybe?) to be able to stop all of them with Ctrl-C.
To be specific, I want to run unit tests with m... |
To run commands concurrently you can use the & command separator.
~$ command1 & command2 & command3
This will start command1, then runs it in the background. The same with command2. Then it starts command3 normally.
The output of all commands will be garbled together, but if that is not a problem for you, that would ... | Run multiple commands and kill them as one in bash |
1,387,949,048,000 |
$ ps -Awwo pid,comm,args
PID COMMAND COMMAND
1 init /sbin/init
2 kthreadd [kthreadd]
3 ksoftirqd/0 [ksoftirqd/0]
5 kworker/u:0 [kworker/u:0]
6 migration/0 [migration/0]
7 cpuset [cpuset]
8 khelper [khelper]
9 netns [netn... |
Brackets appear around command names when the arguments to that command cannot be located.
The ps(1) man page on FreeBSD explains why this typically happens to system processes and kernel threads:
If the arguments cannot be located (usually because it has not been set, as is the case of system processes and/or kernel... | What do the brackets around processes mean? |
1,387,949,048,000 |
Using flock, several processes can have a shared lock at the same time, or be waiting to acquire a write lock. How do I get a list of these processes?
That is, for a given file X, ideally to find the process id of each process which either holds, or is waiting for, a lock on the file. It would be a very good start tho... |
lslocks, from the util-linux package, does exactly this.
In the MODE column, processes waiting for a lock will be marked with a *.
| How to list processes locking file? |
1,387,949,048,000 |
Given file path, how can I determine which process creates it (and/or reads/writes to it)?
|
The lsof command (already mentioned in several answers) will tell you what process has a file open at the time you run it. lsof is available for just about every unix variant.
lsof /path/to/file
lsof won't tell you about file that were opened two microseconds ago and closed one microsecond ago. If you need to watch a... | How to determine which process is creating a file? [duplicate] |
1,387,949,048,000 |
Given a shell process (e.g. sh) and its child process (e.g. cat), how can I simulate the behavior of Ctrl+C using the shell's process ID?
This is what I've tried:
Running sh and then cat:
[user@host ~]$ sh
sh-4.3$ cat
test
test
Sending SIGINT to cat from another terminal:
[user@host ~]$ kill -SIGINT $PID_OF_CAT
cat... |
How CTRL+C works
The first thing is to understand how CTRL+C works.
When you press CTRL+C, your terminal emulator sends an ETX character (end-of-text / 0x03).
The TTY is configured such that when it receives this character, it sends a SIGINT to the foreground process group of the terminal. This configuration can be vi... | Why is SIGINT not propagated to child process when sent to its parent process? |
1,387,949,048,000 |
I'm looking for the process started in Linux which has process ID 0. I know init has PID 1 , which is the first process in Linux, is there any process with PID 0?
|
From the wikipedia page titled: Process identifier:
There are two tasks with specially distinguished process IDs: swapper
or sched has process ID 0 and is responsible for paging, and is
actually part of the kernel rather than a normal user-mode process.
Process ID 1 is usually the init process primarily respons... | Which process has PID 0? |
1,387,949,048,000 |
I found that pidstat would be a good tool to monitor processes. I want to calculate the average memory usage of a particular process. Here is some example output:
02:34:36 PM PID minflt/s majflt/s VSZ RSS %MEM Command
02:34:37 PM 7276 2.00 0.00 349212 210176 7.14 scalpel
(This is ... |
RSS is how much memory this process currently has in main memory (RAM). VSZ is how much virtual memory the process has in total. This includes all types of memory, both in RAM and swapped out. These numbers can get skewed because they also include shared libraries and other types of memory. You can have five hundred i... | Need explanation on Resident Set Size/Virtual Size |
1,387,949,048,000 |
I want to determine which process has the other end of a UNIX socket.
Specifically, I'm asking about one that was created with socketpair(), though the problem is the same for any UNIX socket.
I have a program parent which creates a socketpair(AF_UNIX, SOCK_STREAM, 0, fds), and fork()s. The parent process closes fds... |
Since kernel 3.3, it is possible using ss or lsof-4.89 or above — see Stéphane Chazelas's answer.
In older versions, according to the author of lsof, it was impossible to find this out: the Linux kernel does not expose this information. Source: 2003 thread on comp.unix.admin.
The number shown in /proc/$pid/fd/$fd is ... | Who's got the other end of this unix socketpair? |
1,387,949,048,000 |
In a VM on a cloud provider, I'm seeing a process with weird random name. It consumes significant network and CPU resources.
Here's how the process looks like from pstree view:
systemd(1)───eyshcjdmzg(37775)─┬─{eyshcjdmzg}(37782)
├─{eyshcjdmzg}(37783)
└─{ey... |
eyshcjdmzg is a Linux DDoS trojan (easily found through a Google search). You've likely been hacked.
Take that server off-line now. It's not yours any longer.
Please read the following ServerFault Q/A carefully: How to deal with a compromised server.
Note that depending on who you are and where you are, you may addit... | Process with weird random name consuming significant network and CPU resources. Is someone hacking me? |
1,387,949,048,000 |
I tried ps with different kinds of switches e.g. -A, aux, ef, and so forth but I cannot seem to find the right combination of switches that will tell me the Process ID (PID), Parent Process ID (PPID), Process Group ID (PGID), and the Session ID (SID) of a process in the same output.
|
Here you go:
$ ps xao pid,ppid,pgid,sid | head
PID PPID PGID SID
1 0 1 1
2 0 0 0
3 2 0 0
6 2 0 0
7 2 0 0
21 2 0 0
22 2 0 0
23 2 0 0
24 2 0 0
If you want to see the process... | 'ps' arguments to display PID, PPID, PGID, and SID collectively |
1,387,949,048,000 |
What is the maximum value of the Process ID?
Also, is it possible to change a Process ID?
|
On Linux, you can find the maximum PID value for your system with this:
$ cat /proc/sys/kernel/pid_max
This value can also be written using the same file, however the value can only be extended up to a theoretical maximum of 32768 (2^15) for 32 bit systems or 4194304 (2^22) for 64 bit:
$ echo 32768 > /proc/sys/kernel... | What is the maximum value of the Process ID? |
1,432,100,072,000 |
I want to run multiple commands (processes) on a single shell. All of them have own continuous output and don't stop. Running them in the background breaks Ctrl-C. I would like to run them as a single process (subshell, maybe?) to be able to stop all of them with Ctrl-C.
To be specific, I want to run unit tests with m... |
To run commands concurrently you can use the & command separator.
~$ command1 & command2 & command3
This will start command1, then runs it in the background. The same with command2. Then it starts command3 normally.
The output of all commands will be garbled together, but if that is not a problem for you, that would ... | Run multiple commands and kill them as one in bash |
1,432,100,072,000 |
In the man page, it says:
kill [ -s signal | -p ] [ -a ] [ -- ] pid ...
pid... Specify the list of processes that kill should signal. Each pid can be one of five things:
0 All processes in the current process group are signaled
And I tried like this in bash:
$ man kill &
[1] 15247
$
[1]+ Stopped ... |
Like it says, it sends the signal to all the members of the process group of the caller.
Process groups are used to implement job control in the shell (they can be used for other things, but interactive shell job control is the main reason for their existence).
You'll notice that when you type Ctrl-C, all the processe... | What does kill 0 do actually? [closed] |
1,432,100,072,000 |
In "https://stackoverflow.com/questions/13038143/how-to-get-pids-in-one-process-group-in-linux-os" I see all answers mentioning ps and none mentioning /proc.
"ps" seems to be not very portable (Android and Busybox versions expect different arguments), and I want to be able list pids with pgids with simple and portable... |
You can look at field 5th in output of /proc/[pid]/stat.
$ ps -ejH | grep firefox
3043 2683 2683 ? 00:00:21 firefox
$ < /proc/3043/stat sed -n '$s/.*) [^ ]* [^ ]* \([^ ]*\).*/\1/p'
2683
From man proc:
/proc/[pid]/stat
Status information about the process. This is used by ps(1). It is defi... | Is it possible to get process group ID from /proc? |
1,432,100,072,000 |
I have read that a session's ID is the same as the pid of the process that created the session through the setsid() system call, but I haven't found any information about how a process group ID is set. Is the process group ID the same as the pid of the process that created the process group?
|
In general, yes, the process group ID is equal to the process ID of the process that created the process group — and that process created the process group by putting itself in the group.
You can find this information in the documentation of the setpgid system call, and of its variant setpgrp. The details have histori... | How is a process group ID set? |
1,432,100,072,000 |
So I keep reading everywhere that this command should terminate all child processes of the parent process:
kill -- -$$
Using a negative ID with the kill command references a PGID and from the examples I have seen it appears the PGID of child processes should be the PID of the parent but its not the case on my system.... |
When a process is forked, it inherits its PGID from its parent. The PGID changes when a process becomes a process group leader, then its PGID is copied from its PID. From then on, the new child processes it spawns, and their descendants, inherit that PGID (unless they start new process groups of their own).
In a shell... | Why is the PGID of my child processes not the PID of the parent? |
1,432,100,072,000 |
What's the difference between a process group and a job? If I type pr * | lpr then is it both a process group as well a job?
What exactly is the difference between a process group ID and a job ID?
Edit: I know it appears similar to What is the difference between a job and a process?, but it is slightly different. Als... |
A process group is a unix kernel concept. It doesn't come up very often. You can send a signal to all the processes in a group, by calling the kill system call or utility with a negative argument.
When a process is created (with fork), it remains in the same process group as its parent. A process can move into another... | Difference between process group and job? |
1,432,100,072,000 |
I would like to start a bash script from another bash script, but start it in its own process group just like when you run it from the terminal.
There are a few similar questions, but I can't find an answer that matches my example.
Take these two scripts
$ cat main.sh
#! /usr/bin/env bash
set -e
echo if this has be... |
Assuming Bash, the immediate answer would be to enable "monitor mode" (job control) with set -m, or -m on the command line:
From the man page:
-m Monitor mode. Job control is enabled. This option is on by default for interactive shells on systems that support it (see JOB CONTROL above). All processes run in a... | how can I start a bash script in its own process group |
1,432,100,072,000 |
(Re-posting in unix per the suggestion in https://stackoverflow.com/questions/13718394/what-should-interactive-shells-do-in-orphaned-process-groups)
The short question is, what should a shell do if it is in an orphaned process group that doesn't own the tty? But I recommend reading the long question because it's amusi... |
I agree with your analysis and I agree it sounds like you have to detect whether your process group is orphaned or not.
tcsetattr is also meant to return EIO if the process group is orphaned (and we're not blocking/ignoring SIGTTOU. That might be a less intrusive way than a read on the terminal.
Note that you can repr... | What should interactive shells do in orphaned process groups? |
1,432,100,072,000 |
In POSIX, processes are “related” to each other through two basic hierarchies:
The hierarchy of parent and child processes.
The hierarchy of sessions and process groups.
User processes have a great deal of control over the latter, via setpgid and setsid, but they have very little control over the former—the parent p... |
When I saw this question, I was pretty interested because I know I've seen getppid used before..but I couldn't remember where. So, I turned to one of the projects that I figured has probably used every Linux syscall and then some: systemd. One GitHub search later, and I found two uses that portray some more general us... | Does a process’s parent have any significance from the perspective of its child? |
1,432,100,072,000 |
kill -TERM -PID
is supposed to kill PID and all its child processes.
but this doesn't work on openSUSE, it always tell me that no such process -PID no matter what PID i use.
So if the negative PID option is not supported by this particular version of kill, what is the best way to kill a group of processes?
backgro... |
Does it say "no such PID" or is there an error, - as in does this work?
kill -TERM -- -GPID
Also note, as per (emphasize mine)
man 1:
"[…] When an argument of the form '-n' is given, and it is meant to denote a process group […]"
man 2:
"[…] If pid is less than -1, then sig is sent to every process in the process... | kill a group of processes with negative PID |
1,432,100,072,000 |
Is there a way to change PID, PPID, SID of a running process? It would make sense for the answer to be no, but I'd like to make sure.
|
A process can set its own PGID and SID with the system calls setpgid setsid. The target group/session can't be chosen arbitrarily: setpgid can only move to another process group in the same session, or create a new process group whose PGID is equal to the PID; setsid can only move the process to its own session, makin... | Is there a way to change the process group of a running process? |
1,432,100,072,000 |
I'm learning about the relationship between processes, process groups (and sessions) in Linux.
I compiled the following program...
#include <iostream>
#include <ctime>
#include <unistd.h>
int main( int argc, char* argv[] )
{
char buf[128];
time_t now;
struct tm* tm_now;
while ( true )
{
time( &now );
... |
There is no conflict; a process will by default be in a unique process group which is the process group of its parent:
$ cat pg.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
fork();
printf("pid=%d pgid=%d\n", getpid(), getpgrp());
}
$ make pg
cc pg.c -o pg
$ ./pg
pid=12495 pgid=12495
pid=124... | Why is process not part of expected process group? |
1,432,100,072,000 |
Based on what I have learned so far, a terminal has only one session, and a session has one or more process groups, and a process group has one or more processes.
The following image illustrates this:
I have two questions:
How to move a process from one process group to another?
How to list the processes in each pro... |
From a user's or even a typical programmer's perspective, you don't move processes from one group to another. Organizing process groups is the job of the shell. When you run a job interactively, the shell puts it in its own group. The primary intent of doing that is to kill the whole group (e.g. all the processes in a... | How to move a process from one process group to another, and how to list the processes in each process group? |
1,432,100,072,000 |
I understand from Informit article that sessions and process groups are used to terminate descendant processes on exit and send signals to related processes with job control.
I believe this information can be extracted at any point using the PPID of every process. Do these concepts exist in place just to have a data s... |
Process groups exist primarily to determine which processes started from a terminal can access that terminal. Only processes in the foreground process group may read or write to their controlling terminal; background processes are stopped by a SIGTTIN or SIGTTOU signal.
You can send a signal atomically to all the proc... | What is the purpose of abstractions, session, session leader and process groups? |
1,432,100,072,000 |
I have read that when you press Ctrl+C a SIGINT signal will be sent to the foreground process group.
Can you give me an example of how I can have two or more processes in the foreground process group, because I want to see if all processes will terminate if I press Ctrl+C.
|
Since new processes all belong to the same process group, that of the parent process, have a process start a bunch of processes (fork), and then with appropriate logging and a delay, type Ctrl+C. They all eat a SIGINT.
$ perl -E 'fork for 1..2;say "ima $$"; $SIG{INT}=sub{die "woe $$\n"}; sleep 999'
ima 80920
ima 80922... | Can Ctrl+C send the SIGINT signal to multiple processes? |
1,432,100,072,000 |
I have a statusbar (lemonbar) to which I pipe the output of a couple of scripts (time, battery, volume, etc.). These scripts, and the statusbar itself, are all started in a single bash script statusbar. When the statusbar process is killed, it cleans up after itself by attempting to kill its children, like so:
trap "t... |
You can try using setsid (part of the util-linux package)
in the .xinitrc to start the script in a new session:
setsid statusbar
but will it still receive your signals?
| Start new process group in .xinitrc |
1,432,100,072,000 |
I have read that when you press Ctrl+C, then a SIGINT signal will be sent to the foreground process group.
Now the accepted answer in this question says:
Basically, your signal is received by all foreground processes, ie the
shell and the program,
I have executed cat within bash, and noticed that the PGID for bash... |
That question is about a bash script. You're running bash interactively. This makes a difference for process groups: that's the whole reason why process groups were invented. The intent of a process group is to capture all the processes that are involved in one interactively-started task. So an interactive shell start... | If the shell is running a program, will the shell also receive a SIGINT signal when Ctrl+C is pressed? |
1,432,100,072,000 |
I have a script spawning two zombies. I can kill the group via kill -- -<parent-pid>, but when invoked by the PHP interpreter, that won’t work although killing every single process manually will.
The script is
#!/bin/bash
sleep 1d&
sleep 1d
and the PHP file just invokes it:
<?php
exec("./spawn")
?>
From the shell di... |
The kill command, when given a PID that is < -1, treats it as a process group ID (PGID), not as a process ID. This is documented in info kill:
‘PID < -1’
The process group whose identifier is −PID.
If we take your example again:
$ pstree -p 19935
php(19935)───sh(19936)───spawn(19937)─┬─sleep(19938)
... | Cannot kill process group when invoked by PHP |
1,432,100,072,000 |
I am trying to capture the PID of a function executed in the background, but I seem to get the wrong number.
See the following script:
$ cat test1.sh
#!/bin/bash
set -x
child() {
echo "Child thinks is $$"
sleep 5m
}
child &
child_pid="$!"
echo "Parent thinks pid $child_pid"
sleep 3
kill -- -"$child_pid" ... |
$! gives the correct value.
$$ does not. Use $BASHPID instead
See man bash:
BASHPID
Expands to the process ID of the current bash process. This differs from $$ under certain circumstances, such as subshells that do not require bash to be re-initialized. Assignments to BASHPID have no effect.
Not sure, why your kill ... | PID of background function "$!" gives wrong value |
1,432,100,072,000 |
In The Linux Programming Interface
To see why orphaned process groups are important, we need to view
things from the perspective of shell job control. Consider the
following scenario based on Figure 34-3:
Before the parent process exits, the child was stopped (perhaps because the parent sent it a stop signal).
W... |
Please let me turn mosvy's comment into an answer:
Yes & Yes. You can see the Notes in my answer here for link(s) to the linux source implementing that (the comments from the linux source and the actual code are much better than that prose or my glossing over it)
| Does kernel sending SIGHUP to a process group that becomes orphaned and contains a stopped process terminate all the processes by default? |
1,432,100,072,000 |
Is it possible that a past Progress Group Leader's PID gets reused by an other process and this latter process starts a new Process Group?
In this case the first created process group and the second one have the same PGID, which situation should be avoided I consider.
Does Linux avoid assigning a PID which is a valid ... |
No, that's not possible. It's forbidden by the standard:
The fork() function shall create a new process. The new process
(child process) shall be an exact copy of the calling process
(parent process) except as detailed below:
The child process shall have a unique process ID.
The child process ID also s... | Process Group Leader's PID reused |
1,432,100,072,000 |
From APUE:
A process can set the process group ID of only itself or any of its children.
Furthermore, it can’t change the process group ID of one of its children after that child
has called one of the exec functions.
Why can't it "change the process group ID of one of its children after that child ... |
I do not know the "official" reason but I would guess that the idea is that a process shall not have to expect that its PGID is suddenly changed.
So this is allowed after a fork so that shell pipelines can be set up but after the execve() the new binary find a certain state, and this shall be permanent (until the new ... | Why can't a process change the process group ID of one of its children after that child has called one of the exec functions? |
1,432,100,072,000 |
I am looking at a scenario where I want to run a program / command with sudo as part of a software test. The commands are launched from a Python script based on the subprocess module. I am attempting to avoid having to run the entire test suite with super user privileges.
Let's say for the purpose of this example, it'... |
Scenario 2 can be fixed like this, without the use of setsid:
sudo -b command
This will create a new process group, directly below the system's init process, including the sudo command.
One word of advise, though: If one starts a process group like this with Python's subprocess.Popen, the resulting object's PID (sub... | `sudo setsid command` does not spawn new process group? |
1,432,100,072,000 |
When using setpgrp vi (and other tty programs) work completely different than if setpgrp is not used. Example:
perl -MIPC::Open3 -e '$pid= open3("<&STDIN", ">&STDOUT", ">&STDERR", qw(perl -e),q(exec qw(bash -c),qq(vi foo))); wait'
That works great and calls vi foo. But add setpgrp:
perl -MIPC::Open3 -e '$pid= open3("... |
From setpgrp man page from Darwin/MacOS (BSD-based):
If the calling process is not already a session leader, setpgrp() sets
the process group ID of the calling process to that of the calling
process. Any new session that this creates will have no controlling
terminal.
There's your answer.
| setpgrp causes tty gone |
1,432,100,072,000 |
Suppose that app X is running in the foreground in tmux pane. I'd like to send a given signal, e.g. SIGUSR1, to app X. Can I configure a tmux keybinding to send a signal to the currently-selected pane's foreground process (or process group)?
|
In my Kubuntu ps can give me the ID of the foreground process group on the terminal that the process is connected to. The keyword is tpgid. If I tell ps to query the process identified by tmux as #{pane_pid} then I will get the foreground process group ID in this pane.
The following binding (in ~/.tmux.conf) will make... | Send signal to process in tmux pane |
1,432,100,072,000 |
How can process become a member of a PGRP?
My attempt: Process needs to be a child of a PGRP's leader or we need to use a system call setpgid().
Also, another two questions.
1) How can process become a leader of a group?
I can only think about creating a new process, which will automatically become a leader
2) Can gr... |
I can only think about creating a new process, which will automatically become a leader
False.
#include <stdio.h>
#include <unistd.h>
int main(void) {
pid_t pid;
pid = fork();
printf("%d member of %d\n", getpid(), getpgrp());
return 0;
}
The new process shares the group of the parent:
$ make leaders... | How can process become a member of a process group? |
1,432,100,072,000 |
I searched a lot but didn't find a solution. So it can be silly question.
The format of waitpid is
pid_t waitpid (pid_t pid, int *status, int options)
The pid parameter specifies exactly which process or processes to wait for. Its values fall into
four camps:
< -1
Wait for any child process whose process group ID i... |
You can only wait for children from your process.
If the child changes it's process group id, the new process group id can be used as a negative number with waitpid().
BTW: the function waitpid() is deprecated since 1989. The modern function is: waitid() and it supports what you like:
waitid(idtype, id, infop, opts)
... | Use waitpid for child having groupid 1 |
1,610,233,766,000 |
On Lubuntu 18.04, I run a shell in lxterminal. Its controlling terminal is the current pseudoterminal slave:
$ tty
/dev/pts/2
I would like to know what relations are between my current controlling terminal /dev/pts/2 and /dev/tty.
/dev/tty acts like my current controlling terminal /dev/pts/2:
$ echo hello > /dev/tt... |
The tty manpage in section 4 claims the following:
The file /dev/tty is a character file with major number 5 and minor
number 0, usually of mode 0666 and owner.group root.tty. It is a
synonym for the controlling terminal of a process, if any.
In addition to the ioctl(2) requests supported by the de... | what relations are between my current controlling terminal and `/dev/tty`? |
1,610,233,766,000 |
When slave side of pty is not opened, strace on the process, which does read(master_fd, &byte, 1);, shows this:
read(3,
So, when nobody is connected to the slave side of pty, read() waits for data - it does not return with a error.
But when slave side of pty is opened by a process and that process exits, the read() ... |
On Linux, a read() on the master side of a pseudo-tty will return -1 and set ERRNO to EIO when all the handles to its slave side have been closed, but will either block or return EAGAIN before the slave has been first opened.
The same thing will happen when trying to read from a slave with no master. For the master si... | Why blocking read() on a pty returns when process on the other end dies? |
1,610,233,766,000 |
Linux has 7 virtual consoles, which correspond to 7 device files /dev/tty[n].
Is a virtual console running as a process, just like a terminal emulator? (I am not sure. It seems a virtual console is part of the kernel, and if that is correct, it can't be a process.)
Is a virtual console implemented based on pseudoterm... |
That is incorrect.
There's a terminal emulator program built into the Linux kernel. It doesn't manifest as a running process with open file handles. Nor does it require pseudo-terminal devices. It's layered on top of the framebuffer and the input event subsystem, which it uses internal kernel interfaces to access.... | Is a virtual console running as a process and implemented based on pseudoterminal? |
1,610,233,766,000 |
After having opened the master part of a pseude-terminal
int fd_pseudo_term_master = open("/dev/ptmx",O_RDWR);
there is the file /dev/pts/[NUMBER] created, representing the slave part of he pseudo-terminal.
Ignorant persons, like me might imagine that after having done ptsname(fd_pseudo_term_master,filename_pseudo_ter... |
The old AT&T System 5 mechanism for pseudo-terminal slave devices was that they were ordinary persistent character device nodes under /dev. There was a multiplexor master device at /dev/ptmx. The old 4.3BSD mechanism for pseudo-terminal devices had parallel pairs of ordinary persistent master and slave device nodes ... | Is pseudo terminals ( unlockpt / TIOCSPTLCK ) a security feature? |
1,610,233,766,000 |
I'm trying to figure out how I can reliably loop a read on a pt master I have.
I open the ptmx, grant and unlock it as per usual:
* ptmx stuff */
/* get the master (ptmx) */
int32_t masterfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
if(masterfd < 0){
perror("open");
exit(EXIT_FAILURE);
};
/* grant access to the... |
It's very simple: you should open and keep open a handle to the slave side of the pty in the program handling the master side.
After you got the name with ptsname(3), open(2) it.
I noticed a thread making a call to this: ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icano... | read(2) blocking behaviour changes when pts is closed resulting in read() returning error: -1 (EIO) |
1,610,233,766,000 |
I am want to stream a linux terminal to my own program, and as far as I understand this is done by opening /dev/ptmx to start a new pts, I have tested this and this does indeed work (it creates a new file in /dev/pts). But I am not sure how I am supposed to actually read and write to this terminal. Writing directly t... |
There are two distinct parts to this sort of thing.
As you have determined, you open the master side of the pseudo-terminal and this creates a slave-side device file that can be opened. The ptsname() library function allows something with an open file descriptor for the master side to determine this device name.
The... | documentation on ptmx and pts [closed] |
1,610,233,766,000 |
In a diagram from APUE,
Where is a physical terminal device or virtual console for the terminal emulator read and write with?
what process open, read and write with some physical terminal device or virtual console? Is it the terminal emulator?
|
See What are the responsibilities of each Pseudo-Terminal (PTY) component (software, master side, slave side)? for lots of useful context.
The point of a terminal emulator is to emulate the physical terminals of old. None of the connections in the APUE diagram correspond to anything physical. When it starts a shell, t... | How does a terminal emulator read and write with a physical terminal device? |
1,610,233,766,000 |
From The Linux Programming Interface
under "data transfer" under "communication", we have "byte stream", "message" and "pseudoterminal".
Does pseudoterminal belong to byte stream instead, just like how pipe belongs?
If not, why?
|
Consider the various modes a pseudoterminal can be in: in raw mode, it would behave much like a byte stream, but in cooked mode, it becomes more message-like.
| Does pseudoterminal transfer byte stream or message? |
1,610,233,766,000 |
I'm trying to understand ssh's -t option:
-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-
based programs on a remote machine, which can be very useful, e.g. when implementing
menu services. Multiple -t options force tty allocation, even if ssh h... |
-t option forces [the standard file descriptors] to refer to a TTY device. Is this the correct way of reasoning in order to understand what this option does?
No. The -t option will run the command on the remote machine with its stdin/out/err connected to a pseudo-tty slave instead of a pair of pipes.
Running it in a... | How TTY differs from an ordinary file? [duplicate] |
1,610,233,766,000 |
The Linux Programming Interface says SIGHUP is sent to the controlling process of a terminal
When a terminal window is closed on a workstation. This occurs because the
last open file descriptor for the master side of the pseudoterminal associated
with the terminal window is closed.
My understanding is that a ter... |
My understanding is that a terminal window is created for a slave side, and a master side can have multiple slave sides.
A pseudo terminal has always just one master side and one slave side. It's just a bidirectional pipe with some extra ops [1].
A terminal emulator which can open more than one window/tab will also ... | When closing a terminal emulator window, is the last file descriptor of a slave side or master side closed? |
1,610,233,766,000 |
I have SSH'd into a remote machine. I would like to get the current working directory (and ideally execute commands like ls) on that remote machine, but from outside this process.
Here are my processes
$ ps
49100 ttys001 0:00.21 -zsh
52134 ttys002 0:00.21 -zsh
52171 ttys002 0:00.05 ssh [email protected]
Term... |
If this just isn't possible, would there be something I could do before SSHing into the remote machine that would allow me to do this?
You could start the ssh client in the "connection sharing mode":
ssh -M -S ~/.ssh/%r@%h:%p user@localhost
user@localhost's password:
...
user@localhost$ echo $$
5555
user@localhost$ ... | Get working directory inside SSH client process from outside process |
1,610,233,766,000 |
If run in (pseudo?)text mode as observed under a hupervisor KVM either VMWare, CentOS 8 displays three animated square dots at boot screen:
How to disable this screen to see normal text output also during boot?
|
Edit your grub configuration and remove the "quiet" option from the kernel line.
An easy way to do this is to remove "quiet" from "/boot/grub2/grubenv" but make sure to back up this file first just in case there is a typo which can cause the VM to not boot.
| How to disable three square dots on CentOS 8 boot screen? |
1,610,233,766,000 |
Open xterm, run tty and see pseudo terminal slave file (let's say it is /dev/pts/0).
Then open another xterm and run
$ stty -F /dev/pts/0
speed 38400 baud; line = 0;
lnext = <undef>; discard = <undef>; min = 1; time = 0;
-brkint -icrnl -imaxbel iutf8
-icanon -echo
Then run /bin/sleep 1000 in first xterm. Then run the... |
That's the readline(3) line-editing library, which is usually statically built as part of bash, but is also used by other programs.
Every time it starts reading a command from the user, readline saves the terminal settings, and puts the terminal into "raw" mode [1], so it could be able to handle moving the insertion p... | How bash sets tty attributes before and after running a command? |
1,610,233,766,000 |
OK, I've been googling for hours so I obviously have not been able to understand the answers to the various questions that have already been asked about this subject. I am hoping that, by asking the question again in a more specific way, I will be able to get an answer I can understand.
I have some application runnin... |
I have answered my own question: I found another utility that better provides what I want:
https://github.com/geoffmeyers/interceptty
That package includes a perl script that post-processes the output of interceptty to provide a "pretty" output. I found it quite easy to modify the script to add a timestamp to each ... | How do I use jpnevulator to capture and log the serial traffic between an application and hardware serial port? |
1,610,233,766,000 |
I am writing an executable that uses a 3rd party C library (libmodbus if it matters) to communicate via serial device (in my case, /dev/ttyUSB0 or similar to talk RS-485 via an FTDI chipset based USB-to-RS485 adapter). This executable, based on CLI args, can initiate commands (in my case, act like a modbus client) the... |
You can set up PTY "virtual serial ports" using socat.
socat \
pty,rawer,echo=0,link=/tmp/portA \
pty,rawer,echo=0,link=/tmp/portB
This will create two PTY devices and two symlinks to those devices. On my system, the above command created:
$ ls -l /tmp/port*
lrwxrwxrwx 1 lars lars 11 Jul 24 11:49 /tmp/portA -> /d... | Pseudo terminal for comms between two processes |
1,610,233,766,000 |
On my Ubuntu 20.04.5 machine, I have a Perl script running under userA's account. The script issues this command:
sudo su - userB -c "ssh -l userB 10.0.0.1 ls -tr /some/remote/directory"
(i.e., SSH to a remote host as userB, and then list all the files in /some/remote/directory)
The command works great... except tha... |
if group assigned of that device is "tty", just add the user in that group tty (/etc/group).
Regards
| Pseudo Terminal Error :: "mesg: cannot open /dev/pts/2: Permission denied" |
1,610,233,766,000 |
I'm trying to figure out how exactly CTRL^C sends a SIGINT to a process.
Let's consider a pseudo-terminal system. I'll write what I know (or think I know lol) and please add/replace where needed:
The players are:
Xterm - This is a user space program that reads from the keyboard (using the X window system) and render... |
xterm just writes the ^C character (ASCII 3) to the pseudo-tty master, something you can easily simulate with script (another program which, just like xterm, is managing a master pseudo-tty):
{ sleep 1; printf '\x03'; } | script -qc 'trap "echo SIGINT ma tuer; exit 1" I
NT; cat' /dev/null
^CSIGINT ma tuer
| How exactly a CTRL^C passes a signal to process |
1,610,233,766,000 |
Why pseudo-terminal infers keystrokes from /dev/pts/{number}
(and) X-session infers keystrokes from /dev/input/by-id/{keyboard-device-name}?
I understand pseudo-terminal are running on-top of X-session.
Why is pseudo-terminal so special that it has a separate file location to read/write the data which will be subsequ... |
Why is pseudo-terminal so special that it has a separate file location to read/write the data which will be subsequently displayed in the UI terminal view?
Because there are two fundamentally different views of the user input in play on your desktop.
The display server (X11 or your Wayland compositor) handles all th... | Why pseudo-terminals and X write to different special files |
1,610,233,766,000 |
I have a C program which works with a normal terminal using this code:
int dtr_rts = TIOCM_DTR | TIOCM_RTS; /* out-of-band signal */
...
int comfd = open(COM_PORT, O_RDWR);
...
ioctl(comfd, TIOCMBIS, &dtr_rts);
Now I need to run this program on a pseudo-terminal. How do I read DTR/RTS on master side? Is DTR/RTS set t... |
No, it's not. A pseudo terminal has no way to pass through serial ioctls like TIOCMBIS or TIOCSET.
See also:
Virtual tty client for network telnet/RFC2217 server?
Run a serial connection over SSH
| Is it possible to use TIOCMBIS with pseudo-terminal? |
1,610,233,766,000 |
Since terminal emulators are X11 applications, do they receive input from X11Server if we directly type into the corresponding terminal window?
In that case why would /dev/pts/N directory exists?
Do terminal emulator reject the input events from X Server and directly read from /dev/pts/N?
|
Terminal emulators receive keyboard input as events from the X11 server (or other display server) to which they are connected.
/dev/pts exists so that the terminal emulator can simulate input for the programs running inside it. The emulator receives events from the display server, and translates them into events which... | How do terminal emulators receive input from keyboard [closed] |
1,369,503,686,000 |
What are the practical uses of both pushd and popd when there is an advantage of using these two commands over cd and cd -?
EDIT: I'm looking for some practical examples of uses for both of these commands or reasons for keeping stack with directories (when you have tab completion, cd -, aliases for shortening cd .., e... |
pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.
For example
start up with the following directories:
$ pwd
/home/saml/somedir
$ ls
dir1 dir2 dir3
pushd to dir1
$ pushd dir1
~/somedir/dir1... | How do I use pushd and popd commands? |
1,369,503,686,000 |
The Bash command
cd -
prints the previously used directory and changes to it.
On the other hand, the Bash command
cd ~-
directly changes to the previously used directory, without echoing anything.
Is that the only difference? What is the use case for each of the commands?
|
There are two things at play here. First, the - alone is expanded to your previous directory. This is explained in the cd section of man bash (emphasis mine):
An argument of - is converted to $OLDPWD
before the directory change is attempted. If a non-empty directory name from CDPATH is used, or if ... | Difference between "cd -" and "cd ~-" |
1,369,503,686,000 |
After pushding too many times, I want to clear the whole stack of paths.
How would I popd all the items in the stack?
I'd like to popd without needing to know how many are in the stack?
The bash manual doesn't seem to cover this.
Why do I need to know this? I'm fastidious and to clean out the stack.
|
dirs -c is what you are looking for.
| removing or clearing stack of popd/pushd paths |
1,369,503,686,000 |
cd - can move to the last visited directory. Can we visit more history other than the last one?
|
The command you are looking for is pushd and popd.
You could view a practical working example of pushd and popd from here.
mkdir /tmp/dir1
mkdir /tmp/dir2
mkdir /tmp/dir3
mkdir /tmp/dir4
cd /tmp/dir1
pushd .
cd /tmp/dir2
pushd .
cd /tmp/dir3
pushd .
cd /tmp/dir4
pushd .
dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir... | Do we have more history for cd? |
1,369,503,686,000 |
Is there a difference between the behavior of pushd/popd in bash vs zsh? It seems in zsh cd, cd- behaves exactly the same as pushd/popd (which adds/pops directory automatically when cd) while in bash cd doesn't affect the dir stack.
If someone can give me a pointer that would be great.
|
It depends. In zsh you can configure cd to push the old directory on the directory stack automatically, but it is not the default setting.
As far as I can tell zsh with default settings behaves very similar to bash:
cd somedir
change directory to somedir
save the original directory in OLDPWD
set PWD="somedir"
replac... | pushd, popd vs cd, cd- in bash and zsh |
1,369,503,686,000 |
I would like to use the recently accessed directories list for logging purposes.
Is the directory stack as used by pushd and popd stored somewhere, perhaps as a list of folders in a text file? If so, where?
|
it could be in...
printf %s\\n "${DIRSTACK[@]}" >this_text_file
| How can I view the stack used by `pushd` and `popd`? |
1,369,503,686,000 |
I use pushd to work with multiple directories in bash and zsh. I've aliased dirs to dirs -v so that I get an ordered list when I want to see what's on the directory stack:
chb$ dirs
0 /Volumes/banister/grosste_daever_gh/2013-03-27/reader
1 /tmp/20130618202713/Library/Internet Plug-Ins
2 ~/code/foo/view/static/css
3... |
Bash exposes the directory stack in the DIRSTACK variable. You can also use the command dirs +2 to refer to the second entry on the stack.
More conveniently, ~1 through ~9 refer to the nine topmost entries on the stack. So your example would translate to
chb$ cp ~2/baz.css ~/code/bar/view/static/css/
Zsh has the same... | Refer to an item in `dirs` |
1,369,503,686,000 |
Ok this is a short question. I just happened to know that with pushd command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
|
You may pre-populate the directory stack in your ~/.bashrc file if you wish:
for dir in "$HOME/dir" /usr/src /usr/local/lib; do
pushd -n "$dir" >/dev/null
end
or, if you want to put the directories in an array and use them from there instead:
dirstack=( "$HOME/dir"
/usr/src
/usr/local/lib )
... | Making 'pushd' directory stack persistent |
1,369,503,686,000 |
I am a happy user of the cd - command to go to the previous directory. At the same time I like pushd . and popd.
However, when I want to remember the current working directory by means of pushd ., I lose the possibility to go to the previous directory by cd -. (As pushd . also performs cd .).
How can I use pushd to st... |
You can use something like this:
push() {
if [ "$1" = . ]; then
old=$OLDPWD
current=$PWD
builtin pushd .
cd "$old"
cd "$current"
else
builtin pushd "$1"
fi
}
If you name it pushd, then it will have precedence over the built-in as functions are evaluated bef... | Conflict between `pushd .` and `cd -` |
1,369,503,686,000 |
In a related question somebody states that the directory stack of the pushd command is emptied when your shell terminates. But how is the stack actually stored? I use fish instead of bash and the commands work the same way. I would assume pushd (and popd) works independently of the shell you're using. Or do both shell... |
The directory stack is not stored anywhere permanent. Shell just keeps it in process memory, in an array DIRSTACK (which has restrictions on user modification). It's not even strictly a stack -- bash and ksh allow you to rotate it left and right by specified counts, too.
In Bash, the dirs command clears or shows the s... | How is the pushd directory stack stored? |
1,369,503,686,000 |
Context
linux bash
pushd/popd/dirs
Problem
The problem scenario is very similar to the one stated in this question: removing or clearing stack of popd/pushd paths ... however the goal is not to clear the stack, but rather to prune it. Specifically, the pruning operation is to remove duplicates.
Question
Is there a ... |
This function should remove dups.
dedup(){
declare -a new=() copy=("${DIRSTACK[@]:1}")
declare -A seen
local v i
seen[$PWD]=1
for v in "${copy[@]}"
do if [ -z "${seen[$v]}" ]
then new+=("$v")
seen[$v]=1
fi
done
dirs -c
for ((i=${#new[@]}-1; i>=0; i--))
... | removing duplicates from pushd/popd paths |
1,369,503,686,000 |
When I use popd alone it removes a directory from the stack and takes me to that directory. However, if I do cd $(popd) then no directory is removed from the stack.
Since the process is simply forked and the result is put in place of the shell expansion, why isn't a directory taken off of the stack?
|
The command substitution $(…) runs the command in a subshell. A subshell starts out as an identical¹ copy of the main shell, but from that point on the main shell and the subshell live their own life.
The shell process creates a pipe and forks.
The child runs popd with its output connected to the pipe, then exits.
Th... | Why doesn't shell expansion on popd remove a directory from stack? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.