I take advantage of open-source tooling which predominantly runs on Linux servers, due to this fact usually I discover myself within the terminal working instructions to do varied duties.
On this information – I will provide you with a cheat sheet(type of) of some helpful and fascinating instructions I take advantage of usually.
Look inside a course of
Have a working course of and no thought what it is doing?
Utilizing “strace”, you’ll be able to simply peek inside a working course of and output its log data utilizing the under command:
sudo strace -p 3612485 -e write
Cease and transfer a course of to the background
# Press ctrl+z and kind the next:
bg
Unhealthy web?
Need to run a command in a distant terminal however scared {that a} disconnect may cease the method prematurely?
There are two choices for this, “nohup” or “display screen”. Display opens the terminal in a window so even when your community disconnects – the display screen will nonetheless persist.
The “nohup” command does one thing related besides it retains the method working even when your community disconnects and writes to a log file.
nohup python run_my_awesome_script.py &
We use the “&” to push the method to the background instantly in order that we will proceed working within the terminal. Omitting this may mainly lock your terminal on that command till it finishes.
To make use of screens:
display screen -S MyScreen
To exit and hold the display screen open – enter “ctrl+alt+d”. If you wish to return to the display screen:
# ScreenNameHere - non-compulsory - you probably have just one open display screen.
display screen -r ScreenNameHere
What’s working on port xyz?
lsof -i :5000
# Or
netstat -tupln | grep :5000
# Or - this one could not all the time provide the finest outcomes
# - because it additionally searches course of IDs
ps aux | grep 5000
Will generate an inventory of processes which might be utilizing the desired port.
Sync and duplicate recordsdata
Rsync is a really highly effective instrument for sync recordsdata incrementally, I will not go too in-depth on what the flags imply – please verify the handbook, nonetheless, I’ll cowl some widespread use circumstances for rsync.
Discover and duplicate log recordsdata which might be older than 180 minutes to a backup listing.
discover /var/log/nginx -type f -mmin +180 -exec rsync -av {} /backups/nginx/logs ;
Sync all recordsdata in listing /var/lib/mysql to a distant server. Set the SSH port to “9023”
rsync -rav -e 'ssh -p 9023' --progress /var/lib/mysql/ root@192.168.1.1:/var/lib/mysql2/
Easy copy – if you happen to simply wish to copy recordsdata between two machines and do not take care of incremental syncs.
scp -P9023 -r somefiles/ root@192.168.1.1:/tmp/
Compress and decompress recordsdata
tar -czvf somestuff.tar.gz somestuff/
# And unpack
tar -xzvf somestuff.tar.gz
# Compress a single file
gzip -9 knowledge.xml
# Uncompress single file
gzip -d knowledge.xml.gz
Metrics
# Open system monitor instrument
htop
# View reminiscence utilization
free -m
# View processes
ps aux
# View CPU info
cat /proc/cpuinfo
# Disk area utilization
df -h
# File/listing sizes within the present listing
du -h
# To twenty largest recordsdata & folders in listing
du -h | type -rh | head -n 20
Conclusion
These simply scratch the floor of helpful Linux instructions that you’ll use usually.
I did not cowl “grep”, “awk”, “sed” and so forth, nonetheless, I do have an earlier article that does cowl a few of these – which you’ll take a look at right here.
Hopefully, that is helpful to you, please be happy to remark down under you probably have every other solutions or would really like a future article on a particular space you prefer to me to cowl.