27. 命令: service

‘service‘命令控制服务的启动、停止和重启,它让你能够不重启整个系统就可以让配置生效以开启、停止或者重启某个服务。

在Ubuntu上启动apache2 server:

  1. root@tecmint:~# service apache2 start 
  2. * Starting web server apache2                                                                                                                               
  3.  apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName 
  4. httpd (pid 1285) already running    [ OK ] 

重启apache2 server:

  1. root@tecmint:~# service apache2 restart 
  2. * Restarting web server apache2                                                                                                                               
  3. apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName 
  4. ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName  [ OK ] 

停止apache2 server:

  1. root@tecmint:~# service apache2 stop 
  2. * Stopping web server apache2                                                                                                                                 
  3. apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName 
  4. ... waiting                                                                [ OK ] 

注意:要想使用service命令,进程的脚本必须放在‘/etc/init.d‘,并且路径必须在指定的位置。

如果要运行“service apache2 start”实际上实在执行“service /etc/init.d/apache2 start”.

28. 命令: alias

alias是一个系统自建的shell命令,允许你为名字比较长的或者经常使用的命令指定别名。

我经常用‘ls -l‘命令,它有五个字符包括空格)。于是我为它创建了一个别名‘l'。

  1. root@tecmint:~# alias l='ls -l' 

试试它是否能用:

  1. root@tecmint:~# l 
  2. total 36 
  3. drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary 
  4. drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop 
  5. drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents 
  6. drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads 
  7. drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Music 
  8. drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures 
  9. drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Public 
  10. drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Templates 
  11. drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Videos 

去掉’l'别名,要使用unalias命令:

  1. root@tecmint:~# unalias l 

再试试:

  1. root@tecmint:~# l 
  2. bash: l: command not found 

开个玩笑,把一个重要命令的别名指定为另一个重要命令:

  1. alias cd='ls -l' (set alias of ls -l to cd) 
  2. alias su='pwd' (set alias of pwd to su) 
  3. .... 
  4. (You can create your own) 
  5. .... 

想想多么有趣,现在如果你的朋友敲入‘cd'命令,当他看到的是目录文件列表而不是改变目录;当他试图用’su‘命令时,他会进入当前目录。你可以随后去掉别名,向他解释以上情况。


相关内容