fant0men

Fix broken TTYs

Sep 24th, 2019
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ***
  2.  
  3. switch to a tty: sudo chvt <tty n>
  4.  
  5. show active tty: fgconsole
  6.  
  7. get rid of unused tty: deallocvt
  8.  
  9. ***
  10. https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/network/asynch_tty_hungport.html
  11.  
  12. In this example of clearing a hung port, assume that the hung tty port is tty0.
  13.  
  14. You must have root authority to be able to complete this procedure.
  15.  
  16. Determine whether the tty is currently handling any processes by typing the following:
  17.  
  18. ps -lt tty0
  19. Copy
  20.  
  21. This should return results similar to the following:
  22.  
  23. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
  24. 240001 S 202 22566 3608 0 60 20 781a 444 70201e44 tty0 0:00 ksh
  25. Copy
  26.  
  27. The Process ID (PID) here is 22566. To kill this process, type the following:
  28.  
  29. kill 22566
  30. Copy
  31.  
  32. 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.
  33. 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.
  34.  
  35. kill -9 22566
  36. Copy
  37.  
  38. Determine if any process is attempting to use the tty by typing the following:
  39.  
  40. ps -ef | grep tty0
  41. Copy
  42.  
  43. Note If the ps -ef | grep tty command returns something similar to the following:
  44.  
  45. root 19050 1 0 Mar 06 - 0:00 /usr/sbin/getty /dev/tty
  46. Copy
  47.  
  48. 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.
  49. Note The following command can be used to disable the login process on tty0.
  50.  
  51. pdisable tty0
  52. Copy
  53.  
  54. If the process has been successfully cleared but the tty is still unresponsive, continue to the next step.
  55. Type the following command:
  56.  
  57. fuser -k /dev/tty0
  58. Copy
  59.  
  60. 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.
  61. 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.
  62. 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.
  63. You need to first determine the major and minor device numbers for the tty by typing the following:
  64.  
  65. ls -al /dev/tty0
  66. Copy
  67.  
  68. Your results should look similar to the following:
  69.  
  70. crw-rw-rw- 1 root system 18, 0 Nov 7 06:19 /dev/tty0
  71. Copy
  72.  
  73. 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:
  74.  
  75. /usr/sbin/strreset -M 18 -m 0
  76. Copy
  77.  
  78. If the tty is still unusable, continue to the next step.
  79. 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.
  80. By dropping DCD, detaching and reattaching the cable will in many cases clear hung processes.
  81. To determine the location of the port on which the tty is configured, type the following command:
  82.  
  83. lsdev -Cl tty0
  84. Copy
  85.  
  86. The results should look similar to the following:
  87.  
  88. tty0 Available 00-00-S1-00 Asynchronous Terminal
  89. Copy
  90.  
  91. 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.
  92.  
  93. If the tty is still unusable, continue to the next step.
  94. Flush the port using stty-cxma. Type the following:
  95.  
  96. /usr/lbin/tty/stty-cxma flush tty0
  97. Copy
  98.  
  99. 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.
  100.  
  101. If the tty is still unusable, continue to the next step.
  102. 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.
  103.  
  104. If the tty is still unusable, continue to the next step.
  105. 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:
  106.  
  107. rmdev -l tty0
  108. Copy
  109.  
  110. This command leaves the information concerning the tty in the database but makes the tty unavailable on the system.
  111. The following command reactivates the tty:
  112.  
  113. mkdev -l tty0
  114. Copy
  115.  
  116. 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:
  117.  
  118. errpt -a | pg
  119. Copy
  120.  
  121. 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.
  122. Parent topic:
  123. TTY troubleshooting
  124.  
  125.  
  126. ***
  127. https://www.ostechnix.com/how-to-switch-between-ttys-without-using-function-keys-in-linux/
  128.  
  129. How To Switch Between TTYs Without Using Function Keys In Linux
  130.  
  131. by sk · Published August 22, 2018 · Updated August 19, 2019
  132.  
  133. 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.
  134. Switch Between TTYs In Linux
  135.  
  136. 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.
  137. tty1
  138.  
  139. tty1 in Ubuntu
  140.  
  141. If your system has no X session, just type Alt+Fn key. You don’t need to use CTRL.
  142.  
  143. 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.
  144.  
  145. 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.
  146.  
  147. 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.
  148.  
  149. Let us see print the current tty:
  150.  
  151. $ tty
  152.  
  153. Sample output from my Ubuntu 18.04 LTS server.
  154. tty command output
  155.  
  156. tty command output
  157.  
  158. Now let us switch to tty2. To do so, type:
  159.  
  160. $ sudo chvt 2
  161.  
  162. Remember you need to use “sudo” with chvt command.
  163.  
  164. Now, check the current tty using command:
  165.  
  166. $ tty
  167.  
  168. You will see that the tty has changed now.
  169.  
  170. Similarly, you can switch to tty3 using “sudo chvt 3”, tty4 using “sudo chvt 4” and so on.
  171.  
  172. Chvt command can be useful when any one of your function keys doesn’t work.
  173.  
  174. To view the total number of active virtual consoles, run:
  175.  
  176. $ fgconsole
  177. 2
  178.  
  179. As you can see, there are two active VTs in my system.
  180.  
  181. You can see the next unallocated virtual terminal using command:
  182.  
  183. $ fgconsole --next-available
  184. 3
  185.  
  186. 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.
  187.  
  188. To get rid of unused VTs, just type:
  189.  
  190. $ deallocvt
  191.  
  192. 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.
  193.  
  194. For more details, refer the respective command’s man pages.
  195.  
  196. $ man tty
  197.  
  198. $ man chvt
  199.  
  200. $ man fgconsole
  201.  
  202. $ man deallocvt
  203.  
  204. ***
  205. https://www.linkedin.com/pulse/move-running-process-screen-bruce-werdschinski
  206.  
  207. Move a running process to screen
  208. Published on January 29, 2015
  209. Bruce Werdschinski
  210. Bruce WerdschinskiFollow
  211. Software Developer - I help non-technical founders build their business
  212.  
  213. 182
  214. 15
  215.  
  216. 25
  217.  
  218. A quick guide on how to move a running linux process to the screen terminal multiplexer.
  219.  
  220. 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.
  221.  
  222. 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.
  223.  
  224. 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.
  225.  
  226. 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:
  227.  
  228. Stay back late in the office for an unknown length of time(!!),
  229. Quit the current process, losing any unsaved work and potentially wasting hours of processing, or
  230. Move the running process to a new screen shell. This is the solution that I’ll describe in the rest of this post.
  231.  
  232. Move the running process to a new screen shell
  233.  
  234. 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.
  235.  
  236. The steps we need to take are:
  237.  
  238. Suspend the process
  239. Resume the process in the background
  240. Disown the process
  241. Launch a screen session
  242. Find the PID of the process
  243. Use reptyr to take over the process
  244.  
  245. 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.
  246. Suspend the process
  247.  
  248. The first thing that we need to do is to suspend the process by pressing Ctrl+Z.
  249.  
  250. 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.
  251. Resume the process in the background
  252.  
  253. Type in the bg command. This sends the SIGCONT signal to the process and it’s now happily running in the background.
  254. Disown the process
  255.  
  256. We now run the disown command like so: disown %1
  257.  
  258. Disown removes the process from the table of active jobs, essentially allowing it to be taken over by another session.
  259. Launch a screen session
  260.  
  261. This is pretty easy, we now run the screen command which is where our process will soon be moved to.
  262. Find the PID of the process
  263.  
  264. 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.
  265. Use reptyr to take over the process
  266.  
  267. 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
  268.  
  269. Pro tip: You can combine pgrep and reptyr together with the following syntax: reptyr $(pgrep myprogram)
  270. Summary
  271.  
  272. As a quick reference guide:
  273.  
  274. Suspend the process with Ctrl+Z
  275. Resume the process in the background with bg
  276. Disown the process with disown %1
  277. Launch a screen session with screen
  278. Find the PID of the process using pgrep
  279. Use reptyr to take over the process reptyr
  280.  
  281. Success! Now we’re able to exit screen and reattach from another computer at another time.
  282. The Ubuntu Ptrace gotcha
  283.  
  284. 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.
  285.  
  286. 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
  287.  
  288. 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
  289.  
  290. So for Ubuntu our quick reference guide is:
  291.  
  292. Suspend the process with Ctrl+Z
  293. Resume the process in the background with bg
  294. Disown the process with disown %1
  295. Launch a screen session with screen
  296. Find the PID of the process using pgrep
  297. Enable ptracing with echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  298. Use reptyr to take over the process reptyr
  299. Disable ptracing with echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  300.  
  301. ***
RAW Paste Data