Linux Commands Java Developers Must Master - Learn to Use

"Font color='blue'> Confucius said:" If you want to do a good job, you must first use the weapon. "</font>

Be a positive person

Code, fix bug s, improve yourself

I have a paradise for programming, spring blossoms!

Learning should be happy. In this paradise, I try to make myself use of simple and easy to understand (funny and interesting) expressions to explain knowledge or technology, and make the learning journey full of fun. This is the beginning of blog writing.

Today is the weekend. First of all, I wish you a happy weekend and share a picture taken outside this morning.

Spring in the small park outside the library.

This week I sorted out Linux commands, which I call the magic of Linux. This article reviews some commands (magic), and then uses them (magic) through actual application scenarios.

Narrator:

In Harry Potter movies, there are many magic spells, such as duplicate spells, make objects disappear: disappear without trace spells, make objects come together: tidy up spells, move objects: moving phantom spells, show me the way: directional spells, and so on.

In the magic world of Linux, there are also many command (magic) file copying (cp) magic, file displacement phantom (mv) magic, file aggregation (tar) magic, file disappearance (rm) magic (improper use of file disappearance and disappearance magic is Avatar's life).

Magic Map:

Under linux, "everything is a file"!

1. Disk occupancy

When the server runs for a period of time, there will be accumulation of data. At this point, we use the df magic to see the current use of disk space, as follows:

[aflyun@localhost ~]$ df -h
//File system capacity used Available% mount point
/dev/mapper/centos-root   17G  2.0G   16G   12% /
devtmpfs                 485M     0  485M    0% /dev
tmpfs                    496M     0  496M    0% /dev/shm
tmpfs                    496M  6.8M  490M    2% /run
tmpfs                    496M     0  496M    0% /sys/fs/cgroup
/dev/sda1               1014M  129M  886M   13% /boot
tmpfs                    100M     0  100M    0% /run/user/0

But when you know that there is no space under a mounted disk, how do you know that the files under that directory occupy the most disk space? Then use du magic. As follows:

# View the size of disk space occupied by / home/aflyun directories
[aflyun@localhost ~]$ du -lh --max-depth=1
193M    ./tools
0    ./magic
193M    

Description of each parameter:
#- h or - human-readable improves the readability of information in terms of K, M and G.
#- l or count-links recalculates the files connected to the hardware.
# max-depth= < directory hierarchy > directories that exceed the specified hierarchy are ignored.

<font color='red'> application scenario: Linux server disk space occupied, large file search. </font>

2. Delete files

Delete magic, which is similar to making objects disappear: the Vanishing and Traceless Curse. If this magic is not used properly, it is a black magic (e.g. Avatar Sacrifice).

Normal use:

[aflyun@localhost rmtest]$ touch testrm1 restrm2 
[aflyun@localhost rmtest]$ mkdir -p testrmdir/0317
[aflyun@localhost rmtest]$ mkdir testrmdir1
[aflyun@localhost rmtest]$ cd testrmdir/0317/
[aflyun@localhost 0317]$ touch testrm3
[aflyun@localhost rmtest]$ ll
//Total dosage 0
-rw-rw-r--. 1 aflyun aflyun  0 3 January 17 14:48 restrm2
-rw-rw-r--. 1 aflyun aflyun  0 3 January 17 14:48 testrm1
drwxrwxr-x. 3 aflyun aflyun 18 3 January 17 14:49 testrmdir
drwxrwxr-x. 2 aflyun aflyun  6 3 January 17 14:50 testrmdir1
[aflyun@localhost rmtest]$ 
# 1. Delete a file
[aflyun@localhost rmtest]$ rm  testrm1
# 2. Ask before deleting files
[aflyun@localhost rmtest]$ rm -i restrm2 
rm: Whether to delete ordinary empty files "restrm2"?
# Note: Enter: Delete, ctrl +c: Exit

# 3. Force recursive deletion of files or directories
[aflyun@localhost rmtest]$ rm -f testrmdir
rm: Unable to delete"testrmdir": Is a catalog
[aflyun@localhost rmtest]$ rm -rf testrmdir
[aflyun@localhost rmtest]$ ll
//Total dosage 0
drwxrwxr-x. 1 aflyun aflyun 6 3 January 17 14:50 testrmdir1

//Description of parameters:
-i Ask for confirmation one by one before deleting.
-f Even if the original file attribute is set to read-only, it can be deleted directly without one-by-one confirmation.
-r Delete the catalogue and the following files one by one.

If you don't use it properly, you may run! :

# 1. Strike commands directly to delete log files
[aflyun@localhost ~]$ rm -rf / home/aflyun/logs/
# Did you see any clues? Tip: Do not copy this command to execute!!!
# If you're really curious, try it on your own virtual machine to satisfy your curiosity.
# There's an extra space in this command! 

# 2. Use in scripts to delete log files
[aflyun@localhost ~]$ vim cleanlog.sh
cd ${log_path}
rm -rf *

# Go into the log directory and delete all the logs.
# No problem at first sight? But when catalogues don't exist, that tragedy happens.

<font color='red'> Note: In the production environment, when executing the rm command, you must wipe your eyes, otherwise once you get back to the train, it will be "eternal hatred"! </font>

Friendship suggestions:

The Rm-rf command can be replaced by mv in the production environment, and a regular shell can be written to clean up periodically. The function of recycling bin is simulated.

Reference: Rm-rf * What to do? Running? http://t.cn/Exyx5f5

3. Finding Documents

When you forget where the file / directory is, use find magic.

# 3.1 Find all files under / home/aflyun directory
[aflyun@localhost ~]$ find /home/aflyun/
/home/aflyun/
/home/aflyun/.bash_logout
/home/aflyun/.bash_profile
/home/aflyun/.bashrc
/home/aflyun/tools
/home/aflyun/tools/apache-tomcat-8.5.38.tar.gz
/home/aflyun/tools/jdk-8u191-linux-x64.tar.gz
/home/aflyun/magic
/home/aflyun/magic/happy.log
/home/aflyun/rmtest
/home/aflyun/rmtest/restrm2
/home/aflyun/rmtest/testrmdir1

# 3.2 Find and list all 10M files in the / home/aflyun directory
[aflyun@localhost ~]$ find /home/aflyun -size +5M -exec ls -lh {} \;
-rw-r--r--. 1 root root 9.3M 3 January 17 14:35 /home/aflyun/tools/apache-tomcat-8.5.38.tar.gz
-rw-r--r--. 1 root root 183M 3 January 17 14:35 /home/aflyun/tools/jdk-8u191-linux-x64.tar.gz

//Description of parameters:
# / home/aflyun refers to the current directory. You can also specify the directory/home/happy.
# - size of size file
# + 10M Note that M is capitalized and means more than 10M
# - exec ls-lh {} Pipes are listed and sized
# Ls-lh is listed in the form of k M G
//Note: Make sure you don't run this command in the root directory of your Linux system, as it may cause I/O bottlenecks on your machine.

# 3.3 Find the rest file under / home/aflyun, ignoring case
[aflyun@localhost ~]$ find /home/aflyun/ -iname rest* -type f
/home/aflyun/rmtest/restrm2
/home/aflyun/rmtest/Restrm
//Description of parameters:
# - i Ignore case and case
# - name file name

# 3.4 Find files or directories
[aflyun@localhost ~]$ find /home/aflyun -iname rest* -type f
/home/aflyun/rmtest/restrm2
/home/aflyun/rmtest/Restrm
[aflyun@localhost ~]$ find /home/aflyun -iname rest*  -type d
/home/aflyun/rmtest/restrmdir1

# 3.5 find is used in combination with its friend exec, 3.2 has already been used
# Ignore case, find the file at the beginning of rest, and delete the file
[aflyun@localhost ~]$ ll rmtest/
//Total dosage 0
-rw-rw-r--. 1 aflyun aflyun 0 3 17/15:33 Restrm
-rw-rw-r--. 1 aflyun aflyun 0 3 January 17 14:48 restrm2
drwxrwxr-x. 2 aflyun aflyun 6 3 January 17 14:50 restrmdir1
[aflyun@localhost ~]$ 
[aflyun@localhost ~]$ find /home/aflyun -iname rest* -type f -exec rm -f {} \;
[aflyun@localhost ~]$ 
[aflyun@localhost ~]$ ll rmtest/
//Total dosage 0
drwxrwxr-x. 2 aflyun aflyun 6 3 January 17 14:50 restrmdir1

4. Service Status

In general, we will allow a lot of services on the server. In general, companies will have a server status detection mechanism. Let's use the command to see if the service is started. Take tomcat as an example.

# 4.1 View tomcat service startup status
[aflyun@localhost bin]$ ps -aux | grep tomcat
aflyun    5142  0.0  0.0 113308   688 pts/0    S    15:41   0:00 /bin/sh /home/aflyun/tools/apache-tomcat-8.5.38/bin/catalina.sh start
aflyun    5143  3.4  8.6 2296124 87748 pts/0   Sl   15:41   0:05 /usr/local/java/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/home/aflyun/tools/apache-tomcat-8.5.38/conf/logging.properties 
.....ellipsis

# 4.1.1 Another way to look at service status is to output not 0, representing service startup status.
[aflyun@localhost bin]$ ps -aux | grep -P "/home/aflyun/tools/apache-tomcat-8.5.38/" | grep -v "\sgrep\s" | wc -l
2
[aflyun@localhost bin]$ 

//Note: The service is started, but it does not mean that the service is normal. Sometimes tomcat service view is normal, but the interface can not be accessed, there are false deaths and so on!

# 4.2 Reboot tomcat and encounter 8080 port occupied
java.net.BindException: Address already in use (Bind failed)
    at java.net.PlainSocketImpl.socketBind(Native Method)
    java.net.BindException: Port already in use (Bind failed)
at java.net.PlainSocketImpl.socketBind(Native Method)

# View the usage of port 8080
[aflyun@localhost bin]$ netstat -alnp | grep 8080
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::8080                 :::*                    LISTEN      5143/java

#See which program the port belongs to
lsof -i :8080
[aflyun@localhost bin]$ lsof -i :8080
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    5143 aflyun   49u  IPv6  40442      0t0  TCP *:webcache (LISTEN)
[aflyun@localhost bin]

# Note: 5143 pid is the pid above to view tomat.

5. Log Viewing

In the work, often encounter online project problems, need to check! However, in the vast log file, how to quickly find the wrong place in the log, as well as quickly locate and solve problems, this is a kind of ability, this ability can be exercised in the ordinary work.

# 5.1 View tomcat logs, scroll output
[aflyun@localhost logs]$ tail -f catalina.out 
# 5.2 View tomcat logs containing aflyun characters, scroll output
[aflyun@localhost logs]$ tail -f catalina.out  | grep "java Paradise of Programming Technology"

# 5.3 View the number of rows in tomcat's log that contain "Address is already in use"
# Cat-n File Name | grep Search Name
[aflyun@localhost logs]$ cat -n  catalina.out  | grep "Address already in use"
    83    Caused by: java.net.BindException: Address already in use
   117    Caused by: java.net.BindException: Address already in use
   145     java.net.BindException: Address already in use (Bind failed)
[aflyun@localhost logs]$ 

# 5.4 View tomcat's log with 83 lines of "address already in use" and 10 lines of logs
# Cat-n File Name | Tail-n + Starting Line | Head-n + Query Number of Lines
[aflyun@localhost logs]$ cat -n  catalina.out  | tail -n +73 | head -n +20
    73        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    74        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    75        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    76        at java.lang.reflect.Method.invoke(Method.java:498)
    77        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:309)
    78        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
    79    Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
    80        at org.apache.catalina.connector.Connector.initInternal(Connector.java:995)
    81        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    82        ... 12 more
    83    Caused by: java.net.BindException: Address already in use
    84        at sun.nio.ch.Net.bind0(Native Method)
    85        at sun.nio.ch.Net.bind(Net.java:433)
    86        at sun.nio.ch.Net.bind(Net.java:425)
    87        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    88        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    89        at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:219)
    90        at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:1087)
    91        at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:265)
    92        at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:581)
[aflyun@localhost logs]$ 

Note: <font color='red'> There are many things related to the log. Here it is emphasized that the key point is to log. If something goes wrong, you will enchant all kinds of Linux in time. The log does not output the content of the problem (current request parameters, response parameters, etc.). You are also a "smart woman", because One can't make bricks without straw. Keep a good log! Otherwise, if something goes wrong, you will be distressed and waste a lot of time investigating! Pay attention to every link! </font>

6. Timing Tasks

crontab commands are common in Unix and Unix-like In an operating system, instructions that are periodically executed are set. This command reads instructions from standard input devices and stores them in a "crontab" file for later reading and execution.

The parameters of the crontab command:

- e: Execute a text editor to set the timetable. The built-in text editor is VI. If you want to use another text editor,
Set the VISUAL environment variable to specify which text editor to use (such as setenv VISUAL joe)
- r: Delete the current timetable
 - l: List the current timetable

Note: The minimum execution time for the linux crontab command is one minute. </font>

# 6.1. Write a script and use crontab to execute it every 2s!

[aflyun@localhost crontabtest]$ touch printlog.sh
[aflyun@localhost crontabtest]$ touch print.log
[aflyun@localhost crontabtest]$ vim printlog.sh 
[aflyun@localhost crontabtest]$ cat printlog.sh 
#!/bin/bash
ttime=`date +"%Y-%m-%d %H:%M:%S"`
echo "$ttime,Welcome to Java Paradise of Programming Technology" >>/home/aflyun/crontabtest/print.log
#-------------------------
#  1. Overwrite echo ""> filename
#  2. Additional echo "> filename
[aflyun@localhost crontabtest]$ chmod 755 printlog.sh 
[aflyun@localhost crontabtest]$ ll
//Total dosage 8
-rw-rw-r--. 1 aflyun aflyun 1264 3 17/16:36 print.log
-rwxr-xr-x. 1 aflyun aflyun  132 3 17/16:34 printlog.sh
[aflyun@localhost crontabtest]$ 


# 6.2. Write a timed task
# Crontab-e enters the following statement, then wq saves and exits. After saving, you can use crontab-l to view the timed tasks.
* * * * * /home/aflyun/crontabtest/printlog.sh
* * * * * sleep 1 && /home/aflyun/crontabtest/printlog.sh
* * * * * sleep 2 && /home/aflyun/crontabtest/printlog.sh
...
* * * * * sleep 59 && /home/aflyun/crontabtest/printlog.sh
# 6.3 View Printed Logs
[aflyun@localhost crontabtest]$ cat print.log 
2019-03-17 16:34:01,Welcome to Java Paradise of Programming Technology
2019-03-17 16:34:02,Welcome to Java Paradise of Programming Technology
....

Note: The chronological format of the crontab command is as follows

# f1 f2 f3 f4 f5 program
  • f1 is for minutes, f2 is for hours, f3 is for days in a month, f4 is for months, and f5 is for days in a week.
  • Program represents the program to be executed.
  • When f1 is time, the program is executed every minute, when f2 is time, the program is executed every hour, and so on.
  • When f1 is a-b, it means that it will be executed from minute a to minute b, and when f2 is a-b, it means that it will be executed from hour a to hour b, and so on.
  • When f1 is / n, it is executed every n minutes, f2 is / n, it is executed every n hours, and so on.
  • When f1 is a, b, c,... It means a, b, c,... Minute to execute, f2 is a, b, c,... It means a, b, c,... Hour to execute, and so on.

Summary of this paper

Linux magic (command) related knowledge is temporarily over. If you haven't read the previous series, read the content shared before recommending to read below. I believe that after carefully studying this series, many commands will be grasped with some hints, and can be applied to practical work!

The path of magic practice has come to an end, and there will be time to share it. Welcome to learn and accumulate practical magic skills together.

Recommended reading

Linux commands that Java developers must master (1)
Linux commands that Java developers must master (2)
Linux commands that Java developers must master (3)
Two Linux magic tools that Java developers must master (4)
Linux Commands Java Developers Must Master - Learn to Use (V)

Thank you for your reading. If you think this blog is helpful to you, please comment or like it, so that more people can see it! Wish you a happy day!

<center> <font color='red'> Java programming technology paradise </font>: a public number to share programming knowledge. Learn with the elder driver of the park, and make a little progress every day. </center>
<p/>
Font color='blue'> Scanning attention, background reply to [resources], get the collection of dry goods! 99.9% of the partners liked </font> </center>.
<p/>

Feiyun </center> is getting better every day

Keywords: Linux Java Tomcat Apache

Added by fipp on Sat, 27 Jul 2019 07:04:48 +0300