Skip to content

操作系统和版本相关

bash
# 查看centos版本号
[root@cs ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

# 查看内核版本
[root@cs ~]# uname -r
3.10.0-862.el7.x86_64

# 查看操作系统位数
[root@cs ~]# getconf LONG_BIT
64

端口和进程相关

yum install lsof

查询端口,并根据端口杀进程

# 查询所有已开放的端口
netstat -ntlp   //查看当前所有tcp端口
netstat -ntulp | grep 80   //查看所有80端口使用情况
netstat -ntulp | grep 3306   //查看所有3306端口使用情况
lsof -i tcp:6379


[root@cs ~]# lsof -i tcp:6379
COMMAND    PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
redis-ser 5505 root    4u  IPv4 78273560      0t0  TCP *:6379 (LISTEN)
redis-ser 5505 root    6u  IPv4 78273766      0t0  TCP cs:6379->124.239.61.14:26314 (ESTABLISHED)
[root@cs ~]# kill -9 5505
[root@cs ~]# lsof -i tcp:6379
[root@cs ~]#

关闭防火墙和selinux

# 查看防火墙状态
systemctl status firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
# 禁止开机启动防火墙
systemctl disable firewalld.service
# 启动防火墙
systemctl start firewalld.service
# 防火墙随系统开启启动
systemctl enable firewalld.service

# 关闭selinux
[root@r ~]# sed -i.ori 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

完事后可以重启服务器。

更改主机名

1. 命令行设置
  hostnamectl set-hostname xxx  # 内部也是修改hostname文件
2. 修改hostname文件
  vim /etc/hostname # 更改内容并保存

上面两种无论哪一种都要reboot重启服务器。

vim编辑器设置行号

vim ~/.vimrc
# 编辑如下内容
set number

alias为指定命令设置别名

vim /etc/profile,完事source /etc/profile

bash
alias cls="clear"

nohup 和 &

shell
cat >test.c<<EOF
#include "stdio.h"

int main()
{
	int i =0;
	while(1){
		printf("hello world! %d\n",i++);
		sleep(1);
	}
}
EOF
cat test.c

编译下,并执行:

bash
[root@cs scm]# gcc test.c -o test
[root@cs scm]# ls
test.c test
[root@cs scm]# ./test 
hello world! 0
hello world! 1
hello world! 2

我们通过这个示例来研究下nohup和&

首先./test执行程序,是一个前台程序,也就是每通过xshell连接到服务器之后,就是一个session会话;前台程序在运行时会占用当前session会话,你啥都干不了了,想要在当前窗口结束程序,你只能ctrl+c结束程序运行。而且,当直接关闭xshell软件,随着和服务器的session会话断开,这个前台程序也会随之结束。

但有些程序我们希望它能后台运行,也就是程序运行之后,我们在当前窗口还能做其他操作,我们就需要使用&了。

&

在执行程序的命令后面加&符号,就能让程序在后台运行了。

bash
# 让程序后台运行,我们继续干其他操作,但程序输出内容还会显示在当前会话中,会影响我们做其他操作
./test &

# 只想让程序后台运行,不需要输出,你可以把输出内容指定输出到文件中,具体的语法就是加上这个 >/dev/null 2>&1 固定写法
# /dev/null文件相当于是个黑洞,你扔啥进去都不见了
./test >/dev/null 2>&1 &



[root@cs scm]# ./test >/dev/null 2>&1 &
[1] 6944
[root@cs scm]# ps -aux|grep test
root      6944  4012  0 09:32 pts/1    00:00:00 ./test   # 这个进程就是test程序
root      6954  4012  0 09:32 pts/1    00:00:00 grep --color=auto test

你可以继续在这个终端做其他操作。

但单独使用&还是有缺陷的,因为当我们手动关闭这个session会话时,这个程序还是会随之结束的,所以它只是session级别的后台运行。

如果我们希望即让程序后台执行,哪怕你关闭session会话,也不影响。就需要使用nohup命令了。

nohup结合&使用

单独使用nohup也是前台运行,所以我这里直接演示如何结合&使用,达到最终的后台运行的目的。

bash
# 语法
nohup Command >/dev/null 2>&1 &

[root@cs scm]# nohup ./test >/dev/null 2>&1 &
[1] 12292
[root@cs scm]# ps -ef|grep test
root     12292  4012  0 09:54 pts/1    00:00:00 ./test
root     12333  4012  0 09:54 pts/1    00:00:00 grep --color=auto test

这样就实现了真正的后台运行。