annyoung

ubuntu apache2+tomcat command line tool as python instead of service 본문

프로그래밍

ubuntu apache2+tomcat command line tool as python instead of service

nopsled 2019. 2. 22. 17:27

회사에서 갑자기 VM을 옮기랜다... 까라는대로 까야지...


아무것도 모르는 centos, tomcat, mariadb를 ubuntu, tomcat, mysql로 옮기느라 혼쭐났다. (덕분에 오늘 하루가 빨리 갔다)


열심히 셋팅하고 나니까 느낀점이 apache는 service apache2 start 이런식으로 start하거나 stop과 같은 service가 존재하는데 tomcat은 커맨드라인으로 일일히 해줘야 한다는 것이다.


그래서 귀찮아서 파이썬으로 만들어뒀다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import os, sys, commands
 
class Utils():
    def __init__(self):
        # Define variables
        self.TOMCAT_SHUTDOWN = '/usr/tomcat8/bin/shutdown.sh'
        self.TOMCAT_STARTUP = '/usr/tomcat8/bin/startup.sh'
 
        # Don't modify at this 3 line 11~13
        ALLOW_COMMAND = ['status''start''stop''restart']
        self.apache = False
        self.tomcat = False
 
        # Check parameter
        if not len(sys.argv) == 2:
            print '[-] Not enough param'
            exit()
        elif not sys.argv[1].lower() in ALLOW_COMMAND:
            print '[-] Param type : %s' % ', '.join(ALLOW_COMMAND)
            exit()
 
        # Get status
        shell = commands.getoutput("sudo netstat -ntlp").split('\n')
        for row, row_data in enumerate(shell):
            if not row_data.find('apache'== -1:
                self.apache = True
            elif not row_data.find('java'== -1:
                self.tomcat = True
 
        self.dynamic_loader(sys.argv[1])
        return
 
    def status(self):
        print '[*] Apache status %s' % self.apache
        print '[*] Tomcat status %s' % self.tomcat
        return
 
    def start(self):
        if self.apache:
            print '[*] Apache already loaded.'
        else:
            commands.getoutput('sudo service apache2 start')
            print '[*] Apache started'
 
        if self.tomcat:
            print '[*] Tomcat already loaded.'
        else:
            commands.getoutput(self.TOMCAT_STARTUP)
            print '[*] Tomcat started.'
        return
 
    def stop(self):
        if self.apache:
            commands.getoutput('sudo service apache2 stop')
            print '[*] Apache stopped'
        else:
            print '[*] Apache already stopped.'
 
        if self.tomcat:
            commands.getoutput(self.TOMCAT_SHUTDOWN)
            print '[*] Tomcat stopped.'
        else:
            print '[*] Tomcat already stopped.'
        return
 
    def restart(self):
        commands.getoutput('sudo service apache2 restart')
        print '[*] Apache restarted.'
        if self.tomcat:
            commands.getoutput(self.TOMCAT_SHUTDOWN)
        commands.getoutput(self.TOMCAT_STARTUP)
        print '[*] Tomcat restarted.'
        return
 
    def dynamic_loader(self, func_name):
        func = getattr(self, func_name.lower())
        func()
 
if __name__ == '__main__':
    Utils = Utils()
cs

쓸 사람이 있다면 7,8 라인을 수정해서 사용하면 된다.



1
2
3
4
5
6
7
8
9
10
11
12
dataking@360pia:~$ tail ~/.bashrc
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -/usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -/etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
alias server="python /home/dataking/server.py `$1`"
dataking@360pia:~$
cs


~/.bashrc 맨밑 하단에 위와 같이 추가해주면 커맨드라인으로 사용할 수 있다. (이거 하고나서 shell 로그아웃 안시키고 source ~/.bashrc 사용하면 alias 등록이 바로 된다)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dataking@360pia:~$ server
[-] Not enough param
dataking@360pia:~$ server a
[-] Param type : status, start, stop, restart
dataking@360pia:~$ server status
[*] Apache status True
[*] Tomcat status True
dataking@360pia:~$ server start
[*] Apache already loaded.
[*] Tomcat already loaded.
dataking@360pia:~$ server restart
[*] Apache restarted.
[*] Tomcat restarted.
dataking@360pia:~$ server stop
[*] Apache stopped
[*] Tomcat stopped.
cs

아무튼 alias를 등록하고 해보면 위처럼 보인다



Comments