The usage of systemctl in CentOS 7.2.1511 (replacing service and chkconfig) -- adding VNC Server auto-start

Usage of systemctl in CentOS (replacing service and chkconfig)


1

Copy a file for configuration and rename it to vncserver.service

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


2 Modify

and replace <User> in the file with your current user, and replace %i with 1

, such as the root user configuration, as follows:

[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


Among them, %i can be replaced with 1 constant


3 to make the service take effect, and it is set to automatically start

and update systemctl at boot. This step does not seem to be required, and I have not performed this step.
systemctl daemon-reload



Add service
[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.


View service added
[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


Note the following directory, adding a service (systemctl enable) will generate a data record:
[root@localhost multi-user.target.wants]# pwd
/etc/systemd/system/multi-user.target.wants



4 Start or stop

the service Start the No. 1 window of the service to enable vnc;
systemctl start vncserver or vncserver: 1

to close the No. 1 window:
systemctl stop vncserver (it doesn't work, or it can be connected normally, no stop!!!) Or vncserver -kill :1

5 problem

If there is a problem with startup, pay attention to delete the files under this directory /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 and see if the problem still occurs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326402191&siteId=291194637