当前位置:首页 > Liunx教程 >

CentOS查看进程、杀死进程、启动进程等常用命令

发布时间:2024-03-29 09:24:28 作者:佚名 阅读:(1)

在CentOS系统中,管理进程是系统管理中非常重要的一部分。通过查看、杀死和启动进程,可以确保系统正常运行并及时处理问题。以下是云梦编程为大家介绍的一些在CentOS系统上常用的命令,用于查看、杀死和启动进程,这些命令对系统管理员和运维人员来说非常实用。

CentOS查看进程、杀死进程、启动进程等常用命令

1、查看进程

    ps命令:用于查看当前正在运行的进程。使用ps aux命令可以显示所有进程的详细信息,包括进程ID(PID)、CPU使用率、内存使用量等。

	 ps命令查找与进程相关的PID号:
	 ps a 显示现行终端机下的所有程序,包括其他用户的程序。
	 ps -A 显示所有程序。
	 ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。
	 ps -e 此参数的效果和指定"A"参数相同。
	 ps e 列出程序时,显示每个程序所使用的环境变量。
	 ps f 用ASCII字符显示树状结构,表达程序间的相互关系。
	 ps -H 显示树状结构,表示程序间的相互关系。
	 ps -N 显示所有的程序,除了执行ps指令终端机下的程序之外。
	 ps s 采用程序信号的格式显示程序状况。
 	 ps S 列出程序时,包括已中断的子程序资料。
	 ps -t<终端机编号> 指定终端机编号,并列出属于该终端机的程序的状况。
	 ps u 以用户为主的格式来显示程序状况。
	 ps x 显示所有程序,不以终端机来区分。

    最常用的方法是ps aux,然后再通过管道使用grep命令过滤查找特定的进程,然后再对特定的进程进行操作。

	ps -ef|grep java|grep -v grep    //显示出所有的java进程,去处掉当前的grep进程。

2、杀死进程

    (1)、kill命令:用于终止指定的进程。可以使用进程ID(PID)或进程名称来杀死进程。例如,使用kill PID命令杀死特定进程,其中PID为进程ID。

    (2)、pkill命令:根据进程名称杀死进程。例如,使用pkill process_name命令杀死特定进程,其中process_name为进程名称。

    (3)、killall命令:根据进程名称杀死进程。与pkill命令类似,但killall命令会杀死所有匹配的进程,而不仅仅是一个。

3、启动进程

    (1)、systemctl start service_name命令:用于启动系统服务。将service_name替换为要启动的服务的名称。

    (2)、/etc/init.d/service_name start命令:用于启动系统服务。将service_name替换为要启动的服务的名称。

4、更改档案拥有者

    (1)、命令 :

	chown [-cfhvR] [--help] [--version] user[:group] file... 

    (2)、功能 :

    更改文件或者文件夹的拥有者

    (3)、参数格式 :

	user : 新的档案拥有者的使用者 IDgroup : 新的档案拥有者的使用者群体(group) 
	-c : 若该档案拥有者确实已经更改,才显示其更改动作 
	-f : 若该档案拥有者无法被更改也不要显示错误讯息 
	-h : 只对于连结(link)进行变更,而非该 link 真正指向的档案 
	-v : 显示拥有者变更的详细资料 
	-R : 对目前目录下的所有档案与子目录进行相同的拥有者变更(即以递回的方式逐个变更) 

    (4)、使用实例 :

	chown -R www:www  /opt/www/yundreams  更改目录拥有者为www

5、修改权限

    (1)、命令:

	chmod (change mode) 

    (2)、功能

    改变文件的读写和执行权限。有符号法和八进制数字法。

    (3)、选项

	(1)符号法: 
	命令格式:chmod {u|g|o|a}{+|-|=}{r|w|x} filename 
          u (user)   表示用户本人。 
          g (group)  表示同组用户。 
          o (oher)   表示其他用户。 
          a (all)    表示所有用户。 
          +          用于给予指定用户的许可权限。 
          -          用于取消指定用户的许可权限。 
          =          将所许可的权限赋给文件。 
          r (read)   读许可,表示可以拷贝该文件或目录的内容。 
          w (write)  写许可,表示可以修改该文件或目录的内容。 
          x (execute)执行许可,表示可以执行该文件或进入目录。 

	(2)八进制数字法:   
	命令格式:chmod abc file 
	其中a,b,c各为一个八进制数字,分别表示User、Group、及Other的权限。 
          4 (100)    表示可读。 
          2 (010)    表示可写。 
          1 (001)    表示可执行。 
	若要rwx属性则4+2+1=7; 
	若要rw-属性则4+2=6; 
	若要r-x属性则4+1=5。 

    (4)、使用实例

	# chmod a+rx filename 
	让所有用户可以读和执行文件filename。 
	# chmod go-rx filename 
	取消同组和其他用户的读和执行文件filename的权限。 
	# chmod 741 filename 
	让本人可读写执行、同组用户可读、其他用户可执行文件filename。 
	# chmod -R 755 /home/oracle 
	递归更改目录权限,本人可读写执行、同组用户可读可执行、其他用户可读可执行 

6、修改文件日期

    (1)、命令:touch

    (2)、格式:touch filenae

    (3)、功能:改变文件的日期,不对文件的内容做改动,若文件不存在则建立新文件。

    (4)、实例:touch file

7、链接文件

     (1)、命令:ln (link)

    (2)、格式:

	ln [option] filename linkname 
        ln [option] directory pathname 

    (3)、功能:为文件或目录建立一个链。其中,filename和directory是源文件名和源目录名;linkname和pathname分别表示与源文件或源目录名相链接的文件或目录。

    (4)、选项:

	-b                          like --backup but does not accept an argument
	-d, -F, --directory         allow the superuser to attempt to hard link
                                directories (note: will probably fail due to
                                system restrictions, even for the superuser)
	-f, --force                 remove existing destination files
	-i, --interactive           prompt whether to remove destinations
	-L, --logical               dereference TARGETs that are symbolic links
	-n, --no-dereference        treat LINK_NAME as a normal file if
                                it is a symbolic link to a directory
	-P, --physical              make hard links directly to symbolic links
	-r, --relative              create symbolic links relative to link location
	-s, --symbolic              make symbolic links instead of hard links
	-S, --suffix=SUFFIX         override the usual backup suffix
	-t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create
                                the links
	-T, --no-target-directory   treat LINK_NAME as a normal file always
	-v, --verbose               print name of each linked file
	--help     display this help and exit
	--version  output version information and exit

    (5)、注释:链接的目地在于,对一个文件或目录赋予两个以上的名字,使其可以出现在不同的目录中,既可以使文件或目录共享,又可以节省磁盘空间。

    (6)、实例:ln -s filename linkname

8、显示日期

    (1)、命令:date

    (2)、实例:date

9、显示日历

    (1)、命令:cal (calendar)

    (2)、格式:cal [month] year

    (3)、功能:显示某年内指定的日历

    (4)、例如:cal 2024

10、显示文件头部

    (1)、命令:head

    (2)、格式:head [option] filename

    (3)、功能:显示文件的头部

    (4)、选项:

	-c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading '-', print all but the last
                             K bytes of each file
	-n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading '-', print all but the last
                             K lines of each file
	-q, --quiet, --silent    never print headers giving file names
	-v, --verbose            always print headers giving file names
 	     --help     display this help and exit
 	     --version  output version information and exit

    (5)、使用命令:head filename

11、显示文件尾部

    (1)、命令:tail

    (2)、格式:tail [option] filename

    (3)、功能:显示文件的尾部

    (4)、选项:

	-c, --bytes=K            output the last K bytes; or use -c +K to output
                             bytes starting with the Kth of each file
	-f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                             an absent option argument means 'descriptor'
	-F                       same as --follow=name --retry
	-n, --lines=K            output the last K lines, instead of the last 10;
                             or use -n +K to output starting with the Kth
	--max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                             changed size after N (default 5) iterations
                             to see if it has been unlinked or renamed
                             (this is the usual case of rotated log files);
                             with inotify, this option is rarely useful
      --pid=PID            with -f, terminate after process ID, PID dies
	-q, --quiet, --silent    never output headers giving file names
      --retry              keep trying to open a file if it is inaccessible
	-s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations;
                             with inotify and --pid=P, check process P at
                             least once every N seconds
	-v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit

    (5)、实例:tail filename


通过以上命令,你可以在CentOS系统中方便地查看、杀死和启动进程等。这些命令对于系统管理和故障排除非常有用。希望这篇文章对你有所帮助!

© 2023 - 云梦编程网 版权所有 鲁ICP备2021017318号-4