CentOS 7.2.1511 中systemctl 的用法(代替service 和 chkconfig)- 添加VNC Server自启动

CentOS 7.2.1511 中systemctl 的用法(替代service 和 chkconfig)-- 添加VNC Server自启动
CentOS 中systemctl 的用法(替代service 和 chkconfig)


1 配置

复制一份文件,并改名为vncserver.service

[root@localhost ~]# cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver.service


2 修改

将文件中的<User>用你当前的用户替换,将%i替换为1

如root用户配置,如下:

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target


其中%i可以替换为1常量


3 使服务生效,设置为开机自动启动

更新systemctl,此步,似乎不需要,我到没有执行这一步骤。
systemctl daemon-reload



添加服务
[root@localhost multi-user.target.wants]# systemctl enable vncserver
Created symlink from /etc/systemd/system/multi-user.target.wants/vncserver.service to /usr/lib/systemd/system/vncserver.service.


查看服务已添加
[root@localhost multi-user.target.wants]# ll vncserver*
lrwxrwxrwx. 1 root root 41 Nov  9 21:34 vncserver.service -> /usr/lib/systemd/system/vncserver.service


注意以下这个目录,添加服务(systemctl enable)会产生一条数据记录:
[root@localhost multi-user.target.wants]# pwd
/etc/systemd/system/multi-user.target.wants



4 服务启动或停止

启动该服务用来启用vnc的1号窗口;
systemctl start vncserver  或者 vncserver :1

关闭1号窗口:
systemctl stop vncserver (试了不管用,还是正常能连接上,没有停止!!!)   或者 vncserver -kill :1

5 问题

如果启动会出现问题,注意删除这个目录/tmp/.X11-unix/下的文件:

[root@localhost multi-user.target.wants]# cd /tmp/.X11-unix/
[root@localhost .X11-unix]# pwd
/tmp/.X11-unix
[root@localhost .X11-unix]# ll -a
total 4
drwxrwxrwt.  2 root root   24 Nov  9 21:39 .
drwxrwxrwt. 48 root root 4096 Nov  9 21:39 ..
srwxrwxrwx.  1 root root    0 Nov  9 20:34 X0
srwxrwxrwx.  1 root root    0 Nov  9 20:34 X1
[root@localhost .X11-unix]# rm -rf *


重启reboot一下,看是否还会出现问题。