Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***
- switch to a tty: sudo chvt <tty n>
- show active tty: fgconsole
- get rid of unused tty: deallocvt
- ***
- https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/network/asynch_tty_hungport.html
- In this example of clearing a hung port, assume that the hung tty port is tty0.
- You must have root authority to be able to complete this procedure.
- Determine whether the tty is currently handling any processes by typing the following:
- ps -lt tty0
- Copy
- This should return results similar to the following:
- F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
- 240001 S 202 22566 3608 0 60 20 781a 444 70201e44 tty0 0:00 ksh
- Copy
- The Process ID (PID) here is 22566. To kill this process, type the following:
- kill 22566
- Copy
- Ensure that the process was successfully cleared by typing the command ps -lt tty0. If the process still exists, add the -9 flag to the kill command as shown in the example below.
- Note Do not use the -9 option to kill an slattach process. Killing an slattach process with the -9 flag might cause a slip lock to remain in the /etc/locks file. Delete this lock file to clean up after slattach.
- kill -9 22566
- Copy
- Determine if any process is attempting to use the tty by typing the following:
- ps -ef | grep tty0
- Copy
- Note If the ps -ef | grep tty command returns something similar to the following:
- root 19050 1 0 Mar 06 - 0:00 /usr/sbin/getty /dev/tty
- Copy
- where the "-" is displayed between the date (Mar 06) and the time (0:00), this tty does not have the correct cable. This status indicates that the system login process (getty) is attempting to open this tty, and the open process is hanging because the RS-232 signal Data Carrier Detect (DCD) is not asserted. You can fix this by using the correct null modem adapter in the cabling. When getty can open the tty port, the "-" is replaced by the tty number. For more information on cables, see Attaching the modem with appropriate cables.
- Note The following command can be used to disable the login process on tty0.
- pdisable tty0
- Copy
- If the process has been successfully cleared but the tty is still unresponsive, continue to the next step.
- Type the following command:
- fuser -k /dev/tty0
- Copy
- This will clear any process that can be found running on the port and display the PID. If the tty is still unusable, continue to the next step.
- Use the strreset command to flush outgoing data from the port that is hung due to data that cannot be delivered because the connection to the remote end has been lost.
- Note If the strreset command fixes the hung port, the port has a cable or configuration problem because the loss of the connection to the remote end should have caused buffered data to be flushed automatically.
- You need to first determine the major and minor device numbers for the tty by typing the following:
- ls -al /dev/tty0
- Copy
- Your results should look similar to the following:
- crw-rw-rw- 1 root system 18, 0 Nov 7 06:19 /dev/tty0
- Copy
- This indicates that tty0 has a major device number of 18 and a minor device number of 0. Specify these numbers when using the strreset command as follows:
- /usr/sbin/strreset -M 18 -m 0
- Copy
- If the tty is still unusable, continue to the next step.
- Detach and reattach the cable from the hung tty port. AIX® uses the Data Carrier Detect (DCD) signal to determine the presence of a device attached to the port.
- By dropping DCD, detaching and reattaching the cable will in many cases clear hung processes.
- To determine the location of the port on which the tty is configured, type the following command:
- lsdev -Cl tty0
- Copy
- The results should look similar to the following:
- tty0 Available 00-00-S1-00 Asynchronous Terminal
- Copy
- The third column in the above output indicates the location code of the tty. In this example, S1 indicates the serial port is configured for native serial port 1. For more information on interpreting location codes, see ../devicemanagement/devloccodes.html in Operating system and device management.
- If the tty is still unusable, continue to the next step.
- Flush the port using stty-cxma. Type the following:
- /usr/lbin/tty/stty-cxma flush tty0
- Copy
- This command is intended for the ttys configured on ports of the 8-port and 128-adapters. In some cases, however, it can be used successfully to flush other tty ports.
- If the tty is still unusable, continue to the next step.
- On the keyboard of the hung terminal, hold down the Ctrl key and press Q. This will resume any suspended output by sending an Xon character.
- If the tty is still unusable, continue to the next step.
- A program will sometimes open a tty port, modify some attributes, and close the port without resetting the attributes to their original states. To correct this, bring the tty down to a DEFINED state and then make it available by typing the following:
- rmdev -l tty0
- Copy
- This command leaves the information concerning the tty in the database but makes the tty unavailable on the system.
- The following command reactivates the tty:
- mkdev -l tty0
- Copy
- If the tty is still unusable, consider moving the device to another port and configuring a tty at that location until the system can be rebooted. If rebooting does not clear the port, you most likely have a hardware problem. Check the error report for port hardware problems by entering the following:
- errpt -a | pg
- Copy
- Some of the preceding commands will not work, and they will give a method error indicating that the device is busy. This is because of the process running on the tty. If none of the steps detailed above free the hung tty, as a last resort, reboot the AIX system and flush the kernel so that the process will go away.
- Parent topic:
- TTY troubleshooting
- ***
- https://www.ostechnix.com/how-to-switch-between-ttys-without-using-function-keys-in-linux/
- How To Switch Between TTYs Without Using Function Keys In Linux
- by sk · Published August 22, 2018 · Updated August 19, 2019
- This brief guide describes how to switch between TTYs without using function keys in Unix-like operating systems. Before going further, we will see what TTY is. As mentioned in an answer in AskUbuntu forum, the word TTY came from TeleTYpewriter. Back in the early days of Unix, the user terminals connected to computers were electromechanical teleprinters or teletypewriters( tty in short). Since then, the name TTY has continued to be used for text-only consoles. Nowadays, all text consoles represents virtual consoles, not physical consoles. The TTY command prints the file name of the terminal connected to standard input.
- Switch Between TTYs In Linux
- By default, there are 7 ttys in Linux. They are known as tty1, tty2….. tty7. The 1 to 6 ttys are command line only. The 7th tty is GUI (your X desktop session). You can switch between different TTYs by using CTRL+ALT+Fn keys. For example to switch to tty1, we type CTRL+ALT+F1. This is how tty1 looks in Ubuntu 18.04 LTS server.
- tty1
- tty1 in Ubuntu
- If your system has no X session, just type Alt+Fn key. You don’t need to use CTRL.
- In some Linux editions (Eg. from Ubuntu 17.10 onwards), the login screen now uses virtual console 1 . So, you need to press CTRL+ALT+F3 up to CTRL+ALT+F6 for accessing the virtual consoles. To go back to desktop environment, press CTRL+ALT+F2 or CTRL+ALT+F7 on Ubuntu 17.10 and later.
- What we have seen so far is we can easily switch between TTYs using CTRL+ALT+Function_Key(F1-F7). However, if you don’t want to use the functions keys for any reason, there is a simple command named “chvt” in Linux.
- The “chvt N” command allows you to switch to foreground terminal N, the same as pressing CTRL+ALT+Fn. The corresponding screen is created if it did not exist yet.
- Let us see print the current tty:
- $ tty
- Sample output from my Ubuntu 18.04 LTS server.
- tty command output
- tty command output
- Now let us switch to tty2. To do so, type:
- $ sudo chvt 2
- Remember you need to use “sudo” with chvt command.
- Now, check the current tty using command:
- $ tty
- You will see that the tty has changed now.
- Similarly, you can switch to tty3 using “sudo chvt 3”, tty4 using “sudo chvt 4” and so on.
- Chvt command can be useful when any one of your function keys doesn’t work.
- To view the total number of active virtual consoles, run:
- $ fgconsole
- 2
- As you can see, there are two active VTs in my system.
- You can see the next unallocated virtual terminal using command:
- $ fgconsole --next-available
- 3
- A virtual console is unused if it is not the foreground console, and no process has it open for reading or writing, and no text has been selected on its screen.
- To get rid of unused VTs, just type:
- $ deallocvt
- The above command deallocates kernel memory and data structures for all unused virtual consoles. To put this simply, this command will free all resources connected to the unused virtual consoles.
- For more details, refer the respective command’s man pages.
- $ man tty
- $ man chvt
- $ man fgconsole
- $ man deallocvt
- ***
- https://www.linkedin.com/pulse/move-running-process-screen-bruce-werdschinski
- Move a running process to screen
- Published on January 29, 2015
- Bruce Werdschinski
- Bruce WerdschinskiFollow
- Software Developer - I help non-technical founders build their business
- 182
- 15
- 25
- A quick guide on how to move a running linux process to the screen terminal multiplexer.
- When connecting to a remote linux server most of the time I’m doing a quick task such as looking at log files or doing routine server maintenance. It only takes a few minutes. But sometimes I launch a process that can take many hours to run. Those long running processes are normally related to technical SEO tasks such as multi-gigabyte log file analysis, site crawls and data scraping, or to admin tasks such as database migrations.
- To be as efficient with my own time as possible a common workflow would be to start a long running process in the afternoon, and then later in the evening from home I’d monitor the progress and perform any cleanup as needed. The snag there is that I’d start the operation from my work computer and be in my home office in the evening. To get that to work we use a terminal multiplexer which is software that allows us to create, detach and then reattach linux sessions.
- My terminal multiplexer of choice is screen, although some people prefer tmux. My needs are simple and screen works just fine. Matt Cutts wrote a quick tutorial on screen in 2007 which explains what it is and how it’s used.
- Now, when performing quick and simple tasks I don’t use screen. There’s no point as the session will only last a few minutes. Every now and then though I discover that a running task is taking much longer than expected, or I’ve simply forgotten to use screen to begin with. In that case we’re left with three options:
- Stay back late in the office for an unknown length of time(!!),
- Quit the current process, losing any unsaved work and potentially wasting hours of processing, or
- Move the running process to a new screen shell. This is the solution that I’ll describe in the rest of this post.
- Move the running process to a new screen shell
- Most of the commands that we’ll be using are builtin commands, but you’ll need to install two programs to complete this task. Firstly and rather obviously, screen. The second is reptyr. If you need some assistance with that here’s a great guide on installing software on Linux.
- The steps we need to take are:
- Suspend the process
- Resume the process in the background
- Disown the process
- Launch a screen session
- Find the PID of the process
- Use reptyr to take over the process
- I’ll summarise the steps into a sequence of commands to be used, but first let’s have a quick look at which each step entails.
- Suspend the process
- The first thing that we need to do is to suspend the process by pressing Ctrl+Z.
- When you press Ctrl+Z the TSTP signal is sent to the process. This halts the execution and the kernel won’t schedule any more CPU time to the process.
- Resume the process in the background
- Type in the bg command. This sends the SIGCONT signal to the process and it’s now happily running in the background.
- Disown the process
- We now run the disown command like so: disown %1
- Disown removes the process from the table of active jobs, essentially allowing it to be taken over by another session.
- Launch a screen session
- This is pretty easy, we now run the screen command which is where our process will soon be moved to.
- Find the PID of the process
- Now we need to find the Process ID, (PID) of the process we’d like to take over. The method I use and recommend is pgrep. As an example if our process is called myprogram we can run the command pgrep myprogram which will return the PID.
- Use reptyr to take over the process
- Finally we pass the PID to reptyr to take over the process. If pgrep gave us a PID of 1234, we can now use the command: reptyr 1234
- Pro tip: You can combine pgrep and reptyr together with the following syntax: reptyr $(pgrep myprogram)
- Summary
- As a quick reference guide:
- Suspend the process with Ctrl+Z
- Resume the process in the background with bg
- Disown the process with disown %1
- Launch a screen session with screen
- Find the PID of the process using pgrep
- Use reptyr to take over the process reptyr
- Success! Now we’re able to exit screen and reattach from another computer at another time.
- The Ubuntu Ptrace gotcha
- There’s one gotcha though, with Ubuntu. As a security measure Maverick Meerkat (Ubuntu 10.10) introduced a patch that disallows ptracing of non-child processes by non-root users. This stops reptyr from working.
- To disable this setting permanently you can edit the /etc/sysctl.d/10-ptrace.conf file and setkernel.yama.ptrace_scope to 0. You’ll then need to update the kernel parameters by running sudo sysctl -p /etc/sysctl.d/10-ptrace.conf
- However if you only want to disable ptrace scoping temporarily you can use the following command before reptyr: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope, and then this one after using reptyr: echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- So for Ubuntu our quick reference guide is:
- Suspend the process with Ctrl+Z
- Resume the process in the background with bg
- Disown the process with disown %1
- Launch a screen session with screen
- Find the PID of the process using pgrep
- Enable ptracing with echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- Use reptyr to take over the process reptyr
- Disable ptracing with echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- ***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement