Useful Linux Commands – System

These are some really useful commands to know in the Linux terminal.

System Command

Reboot the System

Reboot the system immediately:

shutdown -r now

Reboot the system after 10 minutes:

shutdown -r +10

Reboot the system after 10 minutes and send a message to all user:

shutdown -r +10 "The system is rebooting in 10 minutes. Please, save your work."

Check last reboot time:

last reboot

Display information about running processes

ps command is used to display information about the processes that are running in the system.

ps

The output will show:

  • PID = the process ID that identify the running process
  • TTY = ┬áthe Terminal type
  • TIME
  • CMD

You can add the -ef option to get the full list (-e option show all the processes, -f option show full details):

ps -ef

or add -eF to view more and more details:

ps -eF

Show the top running processes

The command top show the top running process in your system.

top

The output show information like the user, memory usage, CPU usage, running time, command lunched… You can sort the list pressing SHIFT+O.

top - 16:13:47 up 6 days, 18:56,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 130 total,   1 running, 129 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   949572 total,   120876 free,   140968 used,   687728 buff/cache
KiB Swap:   102396 total,   101380 free,     1016 used.   729356 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND    
  362 stefano   20   0  399000  73304   9344 S   1.0  7.7  65:40.04 java       
 6395 stefano   20   0    8100   3180   2704 R   1.0  0.3   0:00.10 top        
 3070 mongodb   20   0  372996  41672  13452 S   0.3  4.4  37:33.36 mongod     
    1 root      20   0   28108   6116   4884 S   0.0  0.6   0:16.58 systemd    
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.16 kthreadd   
    3 root      20   0       0      0      0 S   0.0  0.0   0:20.76 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+
    7 root      20   0       0      0      0 S   0.0  0.0   0:45.95 rcu_sched  
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh     
    9 root      rt   0       0      0      0 S   0.0  0.0   0:00.11 migration/0
   10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-dr+
   11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0    
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1    
   13 root      rt   0       0      0      0 S   0.0  0.0   0:00.09 migration/1
   14 root      20   0       0      0      0 S   0.0  0.0   0:13.24 ksoftirqd/1
   16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:+
   17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2    

Start or stop a service

To check the current status of a service (with the service name in SERVICE_NAME) run:

service SERVICE_NAME status

For example, to check the status of samba service:

service samba status

To run a service use:

service SERVICE_NAME start

or to stop:

service SERVICE_NAME stop

or to restart:

service SERVICE_NAME restart

If you want to check set status of all running processes:

service --status-all

Know free memory

To know free memory in your system type:

free

The output is displayed in bytes:

              total        used        free      shared  buff/cache   available
Mem:         949572      140332      121768       13716      687472      729976
Swap:        102396        1016      101380

Search for a given string

The grep (general regular expression print) command allow to search for a given string in a file or output of another command.

To search a string in a given file type:

grep -I "STRING_TO_SEARCH" FILE_TO_BE_SCANNED

For example, to search the word “express” (case insensitive, option -i) in the file app.js:

grep -i "express" app.js

The output will be:

var express = require("express");
    session = require("express-session"),
var app = express();
app.use(express.static(path.join(__dirname, "public")));

The grep command is very useful. Please, consider to read the full article here.

Show system environmental variables

To show the system environmental variables use the command export:

export

The output can be really confusing. If you need to search for a given string, combine the export command with grep:

export | grep JAVA

Export a system environmental variable

Use again the export command, with the variable to export:

export VARIABLE_NAME=VALUE

For example to export the JAVA_HOME:

export JAVA_HOME="/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/