Linux users may encounter a situation where a previously running process has crashed, and they want to restart it. However, they might forget the command to restart it. I have also been troubled by this issue for a long time, but I eventually found a solution, which I will introduce here.
-
Use the history command to find the command.
We know that the system records the commands executed by users on Linux, and these records are stored in the ~/.bash_history file. We can use the following command to retrieve them.
[root@qwfys ~]# history
-
Combine with the process ID to find the command.
To combine with the process ID, we first need to obtain the PID of the process, and then check the command line in /proc/$PID/cmdline.Next, let me introduce an example.First, we can find the PID of the process using the command ps -ef.
[root@qwfys ~]# ps -ef UID PID PPID CSTIME TTY TIME CMD
root 1 0 2019 ? 00:23:49 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 2019 ? 00:00:00 [kthreadd]
root 4 2 2019 ? 00:00:00 [kworker/0:0H]
root 6 2 2019 ? 00:00:48 [ksoftirqd/0]
root 7 2 2019 ? 00:00:00 [migration/0]
root 8 2 2019 ? 00:00:00 [rcu_bh]
root 9 2 2019 ? 00:56:02 [rcu_sched]
root 10 2 2019 ? 00:00:00 [lru-add-drain]
root 11 2 2019 ? 00:00:27 [watchdog/0]
root 12 2 2019 ? 00:00:20 [watchdog/1]
root 13 2 2019 ? 00:00:00 [migration/1]
root 14 2 2019 ? 00:03:31 [ksoftirqd/1]
root 16 2 2019 ? 00:00:00 [kworker/1:0H]
root 17 2 2019 ? 00:00:21 [watchdog/2]
root 18 2 2019 ? 00:00:00 [migration/2]
root 19 2 2019 ? 00:00:43 [ksoftirqd/2]
root 21 2 2019 ? 00:00:00 [kworker/2:0H]
root 22 2 2019 ? 00:00:17 [watchdog/3]
root 23 2 2019 ? 00:00:00 [migration/3]
root 24 2 2019 ? 00:03:15 [ksoftirqd/3]
root 26 2 2019 ? 00:00:00 [kworker/3:0H]
root 28 2 2019 ? 00:00:00 [kdevtmpfs]
root 29 2 2019 ? 00:00:00 [netns]
root 30 2 2019 ? 00:00:23 [khungtaskd]
root 31 2 2019 ? 00:00:00 [writeback]
root 32 2 2019 ? 00:00:00 [kintegrityd]
root 33 2 2019 ? 00:00:00 [bioset]
root 34 2 2019 ? 00:00:00 [bioset]
root 35 2 2019 ? 00:00:00 [bioset]
root 36 2 2019 ? 00:00:00 [kblockd]
root 37 2 2019 ? 00:00:00 [md]
root 38 2 2019 ? 00:00:00 [edac-poller]
root 39 2 2019 ? 00:00:00 [watchdogd]
root 46 2 2019 ? 00:00:31 [kswapd0]
root 47 2 2019 ? 00:00:00 [ksmd]
root 48 2 2019 ? 00:00:24 [khugepaged]
root 49 2 2019 ? 00:00:00 [crypto]
root 57 2 2019 ? 00:00:00 [kthrotld]
root 59 2 2019 ? 00:00:00 [kmpath_rdacd]
root 60 2 2019 ? 00:00:00 [kaluad]
root 61 2 2019 ? 00:00:00 [kpsmoused]
root 62 2 2019 ? 00:00:00 [ipv6_addrconf]
root 75 2 2019 ? 00:00:00 [deferwq]
root 152 2 2019 ? 00:00:03 [kauditd]
root 253 2 2019 ? 00:00:00 [ata_sff]
root 275 2 2019 ? 00:00:00 [scsi_eh_0]
root 283 2 2019 ? 00:00:00 [scsi_tmf_0]
root 294 2 2019 ? 00:00:00 [scsi_eh_1]
root 297 2 2019 ? 00:00:00 [scsi_tmf_1]
root 457 2 2019 ? 00:00:00 [ttm_swap]
root 531 2 2019 ? 00:00:02 [kworker/1:1H]
root 537 2 2019 ? 00:00:58 [kworker/0:1H]
root 542 2 2019 ? 00:00:05 [kworker/2:1H]
root 543 2 2019 ? 00:03:48 [jbd2/vda1-8]
root 544 2 2019 ? 00:00:00 [ext4-rsv-conver]
root 606 2 2019 ? 00:00:01 [kworker/3:1H]
root 624 2 2019 ? 00:00:47 /usr/lib/systemd/systemd-journald
root 653 2 2019 ? 00:00:00 /usr/lib/systemd/systemd-udevd
root 992 2 2019 ? 00:00:00 [nfit]
root 1009 2 2019 ? 00:00:10 /sbin/auditd
... (output truncated for brevity)
Next, we enter the directory /proc/$PID to check the corresponding example, which is /proc/30205.
[root@qwfys ~]# ll /proc/30205/
total 0
dr-xr-xr-x 9 root root 0 Jan 15 22:08 .
dr-xr-xr-x 125 root root 0 Nov 30 00:21 ..
dr-xr-xr-x 2 root root 0 Mar 18 10:35 attr
-rw-r--r-- 1 root root 0 Mar 18 10:35 autogroup
-r-------- 1 root root 0 Mar 18 10:35 auxv
-r--r--r-- 1 root root 0 Feb 11 14:25 cgroup
... (output truncated for brevity)
We can see that the directory /proc/$PID contains many files related to the current process. Among them, the file /proc/$PID/cmdline records the detailed command used to start the process.
[root@qwfys ~]# cat /proc/30205/cmdline
/usr/java/jdk1.8.0_201/bin/java -jar -Xdebug -Xnoagent -Xmx1024m -Xms1024m -XX:NewRatio=1 -XX:SurvivorRatio=8 -Dconfig.path=file:/xtwj/biz/xtwj-mall-goods-biz/;file:/xtwj/biz/common.properties -Dlogger.root=/logs/dubbo-jars -Dlogger.module=xtwj-mall-goods-biz/xtwj/biz/xtwj-mall-goods-biz/xtwj-mall-goods-biz.jar
Isn’t it amazing? You can try it out yourself.