Apple UNIX: Difference between revisions
mNo edit summary |
|||
Line 76: | Line 76: | ||
<br> | <br> | ||
Assume you are running the following program which runs in an infinite loop (forever) in a Terminal window. | Assume you are running the following program which runs in an infinite loop (forever) in a Terminal window. | ||
< | <pre> | ||
$ php FormulaExec.php | $ php FormulaExec.php | ||
</ | </pre> | ||
To see the program PID you can use: | To see the program PID you can use: | ||
< | <pre> | ||
$ ps -e | $ ps -e | ||
PID TTY TIME CMD | PID TTY TIME CMD | ||
Line 98: | Line 98: | ||
... | ... | ||
$ | $ | ||
</ | </pre> | ||
If you use: | If you use: | ||
< | <pre> | ||
$ ps -e | grep -i 'formulaexec' | $ ps -e | grep -i 'formulaexec' | ||
10842 ttys001 0:00.12 php FormulaExec.php | 10842 ttys001 0:00.12 php FormulaExec.php | ||
11001 ttys002 0:00.01 grep -i formulaexec | 11001 ttys002 0:00.01 grep -i formulaexec | ||
$ | $ | ||
</ | </pre> | ||
Alternative you can use the {{FormFCT|9|blue|top}} command which opens a monitor on top of the screen (ends by Ctrl-C): | Alternative you can use the {{FormFCT|9|blue|top}} command which opens a monitor on top of the screen (ends by Ctrl-C): | ||
< | <pre> | ||
$ top | $ top | ||
top - 15:43:44 up 41 days, 20:28, 0 users, load average: 4.22, 4.07, 3.55 | top - 15:43:44 up 41 days, 20:28, 0 users, load average: 4.22, 4.07, 3.55 | ||
Line 120: | Line 120: | ||
1 harmfrie 20 0 17956 3220 2664 S 0.0 0.0 0:00.19 bash | 1 harmfrie 20 0 17956 3220 2664 S 0.0 0.0 0:00.19 bash | ||
7 harmfrie 20 0 21684 2328 1900 R 0.0 0.0 0:00.02 top | 7 harmfrie 20 0 21684 2328 1900 R 0.0 0.0 0:00.02 top | ||
</ | </pre> | ||
== Prevent hangup == | == Prevent hangup == | ||
Line 126: | Line 126: | ||
<br>Can I run a command in such a way that I can close the terminal without killing the process? | <br>Can I run a command in such a way that I can close the terminal without killing the process? | ||
The answer is the programs '''nohup''' and '''disown'''. | The answer is the programs '''nohup''' and '''disown'''. | ||
< | <pre> | ||
$ nohup redshift & | $ nohup redshift & | ||
</ | </pre> | ||
or | or | ||
< | <pre> | ||
$ redshift & | $ redshift & | ||
$ disown | $ disown | ||
</ | </pre> | ||
or | or | ||
< | <pre> | ||
$ sleep 1000 | $ sleep 1000 | ||
^Z | ^Z | ||
Line 142: | Line 142: | ||
$ disown | $ disown | ||
$ exit | $ exit | ||
</ | </pre> | ||
All of the above may not work properly, try instead: | All of the above may not work properly, try instead: | ||
< | <pre> | ||
# Suppose for some reason Ctrl+Z is also not working, go to another terminal, find the process id (using ps) and run: | # Suppose for some reason Ctrl+Z is also not working, go to another terminal, find the process id (using ps) and run: | ||
# The command kill -20 (SIGTSTP) will suspend the process | # The command kill -20 (SIGTSTP) will suspend the process | ||
Line 153: | Line 153: | ||
# So now, closing both your terminals won't stop your process. | # So now, closing both your terminals won't stop your process. | ||
# Just remember to do the disown %1 in the first terminal before closing it. | # Just remember to do the disown %1 in the first terminal before closing it. | ||
</ | </pre> | ||
Line 178: | Line 178: | ||
Entering Screen | Entering Screen | ||
< | <pre> | ||
# Enter screen | # Enter screen | ||
$ screen | $ screen | ||
Line 191: | Line 191: | ||
# Re-Attach Screen using | # Re-Attach Screen using | ||
$ screen -r | $ screen -r | ||
</ | </pre> | ||
Revision as of 14:38, 10 March 2019
The underwater operating system of Apple SnowLeopard is based on Unix. So use the power of Unix in Apple Mac.
Useful Unix Utilities
Program | Description | Usage Example |
---|---|---|
bc | Basic Calculator, type 2+2 and press return. | |
kill | Max OS X offers no shortage of ways to cut the cord on programs that seems to be locked up or running amok. Use the Activity Monitor or use kill.
First get the ID number running top. |
|
open | Launch programs | |
ps | Process Status (ps) is another way to get a quick look at all running programs. For the best results use the -e and the -f flags. | ps -ef |
shutdown | Shutdown with the -h flags has its advantages, because it gives the user more control. | shutdown -h |
tar, gzip, zip | Compressed Archive files | |
top | Running top (table of processes) lists every program currently running. | top -u |
xattr | Manage the extended attributes of Mac files, invisible metadata describing all kind of characteristics of every file. | |
alias | Shortcuts for commands | alias la='ls -Al' |
nano, emacs, vim | editors | |
grep | Grep is a filter and is used in combination with other programs. | |
find | Terrible powerful program with a lot of features. | find . -name "fn" -type f -print 2>/dev/null |
mdfind | MedaData find | mdfind 'kMDItenFlashOnOff == "1"' |
launchd | Launching System Programs | |
ftp | File Transfer Program |
Example Process Finding
If you are running a terminal program, you may wanna find if it is still running using one of the above tools.
Assume you are running the following program which runs in an infinite loop (forever) in a Terminal window.
$ php FormulaExec.php
To see the program PID you can use:
$ ps -e PID TTY TIME CMD 1 ?? 2:44.97 /sbin/launchd 46 ?? 0:48.23 /usr/sbin/syslogd ... 528 ttys000 0:01.81 login -pfl HaFrMpro /bin/bash -c exec -la bash /bin/bash 542 ttys000 0:00.08 -bash 3002 ttys000 0:00.01 tail -f -n 64 /Applications/MAMP/logs/php_error.log 536 ttys001 0:01.76 login -pfl HaFrMpro /bin/bash -c exec -la bash /bin/bash 644 ttys001 0:00.18 -bash 10842 ttys001 0:00.11 php FormulaExec.php 537 ttys002 0:01.77 login -pfl HaFrMpro /bin/bash -c exec -la bash /bin/bash 668 ttys002 0:00.76 -bash 10969 ttys002 0:00.00 ps -e ... $
If you use:
$ ps -e | grep -i 'formulaexec' 10842 ttys001 0:00.12 php FormulaExec.php 11001 ttys002 0:00.01 grep -i formulaexec $
Alternative you can use the top command which opens a monitor on top of the screen (ends by Ctrl-C):
$ top top - 15:43:44 up 41 days, 20:28, 0 users, load average: 4.22, 4.07, 3.55 Tasks: 2 total, 1 running, 1 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.5 us, 6.8 sy, 37.5 ni, 54.8 id, 0.1 wa, 0.0 hi, 0.4 si, 0.0 st KiB Mem: 32142684 total, 30160744 used, 1981940 free, 1712 buffers KiB Swap: 0 total, 0 used, 0 free, 24802844 cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 harmfrie 20 0 17956 3220 2664 S 0.0 0.0 0:00.19 bash 7 harmfrie 20 0 21684 2328 1900 R 0.0 0.0 0:00.02 top
Prevent hangup
Closing the terminal kills the process.
Can I run a command in such a way that I can close the terminal without killing the process?
The answer is the programs nohup and disown.
$ nohup redshift &
or
$ redshift & $ disown
or
$ sleep 1000 ^Z [1]+ Stopped sleep 1000 $ bg $ disown $ exit
All of the above may not work properly, try instead:
# Suppose for some reason Ctrl+Z is also not working, go to another terminal, find the process id (using ps) and run: # The command kill -20 (SIGTSTP) will suspend the process $ kill -20 PID # The command kill -18 (SIGCONT) will resume the process, in background. $ kill -18 PID # So now, closing both your terminals won't stop your process. # Just remember to do the disown %1 in the first terminal before closing it.
See
- Computerhope.com ups
Server Broken Pipes
For keeping the connection alive, you can check in /etc/ssh_config the line where it says ServerAliveInterval, that tells you how often (in seconds) your computer is gonna send a null packet to keep the connection alive.
If you have a 0 in there that indicates that your computer is not trying to keep the connection alive (it is disabled), otherwise it tells you how often (in seconds) it is sending the aforementioned packet.
Try putting 120 or 240, if it is still killing your connection, you can go lower, maybe to 5, if with that number it doesn't happen, maybe it is your router who is dumping the connection to free memory.
Example
ServerAliveInterval 10
See
- Apple StackExchange, Prevent ssh session from hanging.
Screen
Screen is a full-screen software program that can be used to multiplexes a physical console between several processes (typically interactive shells). It offers a user to open several separate terminal instances inside a one single terminal window manager.
Entering Screen
# Enter screen $ screen # Do something such as installing something $ git clone xxxx # Detach your screen using Ctrl-A-d [detached] $ # Re-Attach Screen using $ screen -r
The screen application is very useful, if you are dealing with multiple programs from a command line interface and for separating programs from the terminal shell. It also allows you to share your sessions with others users and detach/attach terminal sessions.
Leaving Screen
There are 2 (two) ways to leaving the screen.
- Ctrl-A and D to detach the screen.
- Ctrl-A and K to kill the screen.
That’s some of screen usage on daily basis. There are still a lot of features inside the screen command. You may see screen man page for more detail.
- TecMint Screen. Command examples to manage Linux terminals TecMint Screen.
cron replacement
Cronjobs and other deamons are not allowed on most Hosting Service Providers. An alternative maybe EasyCron and setcronjob
- easycron, has a free plan for simple cronjobs.
- setcronjob, not free and has the same functionality as easycron.