CentOS7: 04-PhantomJS systemd Service Wrapper

  • systemd makes simple services so much easier to deal with

 

Create phantomjs.service

touch /etc/systemd/system/phantomjs

 

[setup@c7 ~]$ cd /etc/systemd
[setup@c7 systemd]$ ls
bootchart.conf  coredump.conf  journald.conf  logind.conf  system  system.conf  user  user.conf
[setup@c7 systemd]$ cd system


[setup@c7 system]$ sudo touch phantomjs.service
[sudo] password for setup:

 

[setup@c7 system]$ ls -la
total 8
drwxr-xr-x. 12 root root 4096 Nov  1 22:23 .
drwxr-xr-x.  4 root root  151 Nov  1 18:39 ..
drwxr-xr-x.  2 root root   57 Nov  1 18:30 basic.target.wants
lrwxrwxrwx.  1 root root   41 Nov  1 18:30 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx.  1 root root   46 Nov  1 18:30 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx.  1 root root   57 Nov  1 18:30 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx.  1 root root   37 Nov  1 18:35 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x.  2 root root   87 Nov  1 18:30 default.target.wants
drwxr-xr-x.  2 root root   32 Nov  1 18:30 getty.target.wants
drwxr-xr-x.  2 root root   35 Nov  1 18:30 local-fs.target.wants
drwxr-xr-x.  2 root root 4096 Nov  1 18:30 multi-user.target.wants
drwxr-xr-x.  2 root root   48 Nov  1 18:30 network-online.target.wants
-rw-r--r--.  1 root root    0 Nov  1 22:23 phantomjs.service
drwxr-xr-x.  2 root root    6 Oct 16 14:35 php-fpm.service.d
drwxr-xr-x.  2 root root   29 Nov  1 18:30 sockets.target.wants
drwxr-xr-x.  2 root root  217 Nov  1 18:30 sysinit.target.wants
drwxr-xr-x.  2 root root   44 Nov  1 18:30 system-update.target.wants

 

CHMOD phantomjs.service


[setup@c7 system]$ sudo chmod 777 phantomjs.service
[setup@c7 system]$ nano phantomjs.service

 

 

Edit phantomjs.service

nano /etc/systemd/system/phantomjs.service
  • commented the following lines - not sure of their function
    • #After=network.target
    • #WantedBy=multi-user.target
    •  
[Unit]
Description=PhantomJS Service
#After=network.target

[Service]
Type=simple
User=setup
ExecStart=/var/www/tools/phantomjs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs --webdriver  8643
Restart=on-abort

[Install]
#WantedBy=multi-user.target

 

Reload the daemons

[setup@c7 system]$ sudo systemctl daemon-reload
[sudo] password for setup:

 

Start phantomjs Service

[setup@c7 system]$ sudo systemctl start phantomjs
[setup@c7 system]$ netstat -ntlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8643            0.0.0.0:*               LISTEN      1539/phantomjs      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   

 

Using Kill <pid> to Stop phantomjs listener

[setup@c7 system]$ ps
  PID TTY          TIME CMD
 1381 pts/0    00:00:00 bash
 1555 pts/0    00:00:00 ps

[setup@c7 system]$ kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

[setup@c7 system]$ kill 1539

[setup@c7 system]$ ps
  PID TTY          TIME CMD
 1381 pts/0    00:00:00 bash
 1557 pts/0    00:00:00 ps

 

[setup@c7 system]$ netstat -ntlp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   

 

 

Starting the Service & Checking NetStat

 

sudo systemctl start phantomjs

 

 1557 pts/0    00:00:00 ps
[setup@c7 system]$ netstat -ntlp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
[setup@c7 system]$ sudo systemctl start phantomjs
[setup@c7 system]$ netstat -ntlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8643            0.0.0.0:*               LISTEN      1566/phantomjs      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22 

 

 


 

Stopping the service

sudo systemctl stop phantomjs

 

[setup@c7 system]$ netstat -ntlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8643            0.0.0.0:*               LISTEN      1566/phantomjs      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
[setup@c7 system]$ sudo systemctl stop phantomjs
[sudo] password for setup:
[setup@c7 system]$ netstat -ntlp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*  

 


 

#!/bin/bash

# /var/www/tools/phantomjs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs --webdriver 8643


echo starting phantomjs service....
sudo systemctl start phantomjs
echo waiting before netstat....
ping -c 10 localhost
echo netstat......
netstat -ntlp

 

 

#!/bin/bash

echo stopping phantomjs service....
sudo systemctl stop phantomjs
echo waiting before netstat....
ping -c 10 localhost
echo netstat......
netstat -ntlp