<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Neuronworks &#187; server</title>
	<atom:link href="http://blog.neuronworks.net/tag/server/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.neuronworks.net</link>
	<description></description>
	<lastBuildDate>Sun, 04 Dec 2011 05:10:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Membuat Application Control Script</title>
		<link>http://blog.neuronworks.net/2008/12/29/membuat-application-control-script/</link>
		<comments>http://blog.neuronworks.net/2008/12/29/membuat-application-control-script/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 06:07:58 +0000</pubDate>
		<dc:creator>alam</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.neuronworks.net/?p=16</guid>
		<description><![CDATA[Background Sekitar seminggu yang lalu saya ditugaskan untuk membuat sebuah skrip yang digunakan untuk mengontrol service di Linux. Skrip tersebut harus bisa menjalankan proses server apabila servernya belum dijalankan, mampu memberhentikan service tersebut dan juga merestart server, baik dari keadaan sudah dijalankan atau belum. Server executablenya disini merupakan aplikasi java dalam bentuk java archive. Work]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>Sekitar seminggu yang lalu saya ditugaskan untuk membuat sebuah skrip yang digunakan untuk mengontrol service di Linux. Skrip tersebut harus bisa menjalankan proses server apabila servernya belum dijalankan, mampu memberhentikan service tersebut dan juga merestart server, baik dari keadaan sudah dijalankan atau belum. Server executablenya disini merupakan aplikasi java dalam bentuk java archive.</p>
<h2>Work Flow</h2>
<p>Apabila user menjalankan skrip tersebut, skripnya akan mengecek apakah user memberikan parameter yang dibutuhkan, yaitu start, restart atau stop. Apabila user memasukkan parameter yang tidak dikenali, skrip akan menampilkan pesan yang memberitahukan bahwa parameter tidak dikenal. Apabila user memberikan parameter <strong>start</strong> skrip akan memeriksa apakah file <strong>server.pid</strong> yang berisi pid dari aplikasi tersebut ada atau tidak. Apabila ada skrip akan menampilkan pesan bahwa aplikasi tersebut sudah jalan dengan pid yang dambil dari file <strong>server.pid</strong>. Apabila belum skrip akan menjalankan aplikasi tersebut dan menyimpan pid dari process itu ke file <strong>server.pid</strong>. Bila parameter yang diberikan parameter <strong>stop</strong> maka skrip akan memeriksa apakah aplikasinya memang berjalan dengan mengecek file <strong>server.pid</strong>. Apabila ada skrip akan memberhentikan process tersebut dengan bantuan pid yang diambil dari file <strong>server.pid</strong>, dan setelah process-nya berhenti file <strong>server.pid</strong> tersebut akan dihapus. Apabila file <strong>server.pid</strong> tidak ditemukan maka skrip akan menampilkan pesan bahwa aplikasi yang dimaksud tidak berjalan. Parameter <strong>restart</strong> akan menjalankan fungsi <strong>stop</strong> terlebih dahulu dilanjutkan dengan fungsi <strong>start</strong>.</p>
<h2>Code</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;">#License GPL Version 2</span>
<span style="color: #666666; font-style: italic;">#script to start, stop and restart server application</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#check if user enter the parameter</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #780078;">`basename $0`</span> {start | restart | stop}&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#variable that hold java binary</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVABIN</span>=<span style="color: #000000; font-weight: bold;">`/</span>app<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>jdk1.6.0_03<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #666666; font-style: italic;">#variable that hold base directory</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">BASEDIR</span>=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>server
<span style="color: #666666; font-style: italic;">#variable that hold jar file or server application location</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JARFILE</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.jar
<span style="color: #666666; font-style: italic;">#variable that hold date time format for renaming log file</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DTM</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d-<span style="color: #000000; font-weight: bold;">%</span>H<span style="color: #000000; font-weight: bold;">%</span>M<span style="color: #000000; font-weight: bold;">%</span>S<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #666666; font-style: italic;">#variable that hold log file location</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LOG</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.log
<span style="color: #666666; font-style: italic;">#variable that hold file containing server pid</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PIDFILE</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.pid
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> startServer<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDFILE</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server already running with pid:<span style="color: #780078;">`cat $PIDFILE`</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #007800;">$JAVABIN</span> <span style="color: #660033;">-jar</span> <span style="color: #007800;">$JARFILE</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #007800;">PID</span>=<span style="color: #007800;">$!</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$PID</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$PIDFILE</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server started with pid:<span style="color: #007800;">$PID</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stopServer<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;">#check if server already running</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDFILE</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PID</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$PIDFILE</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server running with pid:<span style="color: #007800;">$PID</span>&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> <span style="color: #007800;">$PID</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #007800;">$PIDFILE</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$LOG</span> <span style="color: #007800;">$LOG</span>-<span style="color: #007800;">$DTM</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server stopped&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server are not running&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$BASEDIR</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;start&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server start command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to start the server here</span>
startServer
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;stop&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server stop command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to stop the server here</span>
stopServer
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;restart&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server restart command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to restart the server here</span>
stopServer
startServer
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;unknown command <span style="color: #780078;">`basename $1`</span>, supported command are: start, restart, stop&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p>Simpan file ini sebagai server.sh. Beri permission execute dengan:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x server.sh</pre></div></div>

<p>Jalankan skrip ini dengan perintah:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>server.sh</pre></div></div>

<h2>Explanation</h2>
<p>Baris yang pertama berupa:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span></pre></div></div>

<p>merupakan opsional. Saya lupa lagi apa istilahnya, intrepeter directive atau apa. Baris ini memberitahukan kepada Linux intrepreter apa yang digunakan untuk mengeksekusi skrip tersebut, dalam hal ini kita meminta agar skrip ini dieksekusi menggunakan shell default yang memiliki path <strong>/bin/sh</strong>. Di sistem operasi openSUSE <strong>/bin/sh</strong> merupakan symlink untuk Shell Bash <strong>/bin/bash</strong>. Apabila kita tidak menggunakan baris ini user harus menjalankan shell dengan skrip ini sebagai parameter seperti dibawah ini:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sh</span> server.sh</pre></div></div>

<p>Baris berikutnya:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#License GPL Version 2</span>
<span style="color: #666666; font-style: italic;">#script to start, stop and restart server application</span></pre></div></div>

<p>merupakan baris komentar. Baris yang diawali dengan <strong>#</strong> akan diabaikan oleh shell.</p>
<p>Potongan kode berikutnya:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#check if user enter the parameter</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #780078;">`basename $0`</span> start | restart | stop&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>untuk melakukan pengecekan apakah user mengeksekusi skrip beserta parameter yang dibutuhkan.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Operator <strong>-z</strong> digunakan untuk mengecek apakah stringnya null atau tidak. <strong>$1</strong> merupakan built-in variable yang dapat kita gunakan untuk mengakses parameter pertama yang dijalankan bersama dengan skrip. Untuk mengakses parameter kedua dan seterusnya ganti nilai 1 dengan 2 dan seterusnya sampai 9. Apabila kosong, yang berarti user tidak memasukkan parameter yang kita harapkan, baris ini menghasilkan nilai true yang berarti akan menjalankan kode berikutnya:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #780078;">`basename $0`</span> {start | restart | stop}&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<p><strong>echo</strong> digunakan untuk mencetak ke alat output standar, dalam hal ini layar monitor. <strong>exit</strong> akan menyebabkan skrip untuk keluar.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#variable that hold java binary</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVABIN</span>=<span style="color: #000000; font-weight: bold;">`/</span>app<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>jdk1.6.0_03<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #666666; font-style: italic;">#variable that hold base directory</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">BASEDIR</span>=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>server
<span style="color: #666666; font-style: italic;">#variable that hold jar file location</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JARFILE</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.jar
<span style="color: #666666; font-style: italic;">#variable that hold date time format for renaming log file</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DTM</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d-<span style="color: #000000; font-weight: bold;">%</span>H<span style="color: #000000; font-weight: bold;">%</span>M<span style="color: #000000; font-weight: bold;">%</span>S<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #666666; font-style: italic;">#variable that hold log file location</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LOG</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.log
<span style="color: #666666; font-style: italic;">#variable that hold file containing application pid</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PIDFILE</span>=<span style="color: #007800;">$BASEDIR</span><span style="color: #000000; font-weight: bold;">/</span>server.pid</pre></div></div>

<p>Potongan kode diatas melakukan insialisasi variabel-variabel yang akan kita gunakan dalam skrip ini. <strong>export</strong> digunakan untuk menset variabel shell. Pastikan tidak ada spasi antara nama variabel, <strong>=</strong> dan isi variabelnya. Untuk mengakses variabel tambahkan <strong>$</strong> dollar sign didepan nama variabel yang dimaksud.</p>
<p>Perhatikan baris kode ini:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">BASEDIR</span>=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>server</pre></div></div>

<p>Variable <strong>$HOME</strong> belum pernah diinisialisasi, lalu bagaimana bisa ada nilainya? Shell Bash memiliki beberapa variabel global yang dapat digunakan oleh process shell-shell yang dijalankan dibawahnya. Dalam hal ini <strong>$HOME</strong> berisi path home untuk user. Untuk melihat variabel-variabel apa saja yang tersedia, jalankan perintah <strong>env</strong> di command line. Pastikan variabel yang dinisialisasi belum digunakan untuk menghindari terjadinya konflik variabel.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> startServer<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDFILE</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server already running with pid:<span style="color: #780078;">`cat $PIDFILE`</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #007800;">$JAVABIN</span> <span style="color: #660033;">-jar</span> <span style="color: #007800;">$JARFILE</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #007800;">PID</span>=<span style="color: #007800;">$!</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$PID</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$PIDFILE</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server started with pid:<span style="color: #007800;">$PID</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Potongan kode diatas merupakan fungsi yang digunakan untuk menjalankan server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> startServer<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span></pre></div></div>

<p>Merupakan syntax untuk membuat fungsi. Syntax ini cukup jelas penggunaannya.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDFILE</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server already running with pid:<span style="color: #780078;">`cat $PIDFILE`</span>&quot;</span></pre></div></div>

<p>Operator <strong>-f</strong> digunakan untuk mengecek apakah file yang dimaksud ada yang akan mengembalikan nilai true dan mencetak process id (PID) dari aplikasi server tersebut.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #007800;">$JAVABIN</span> <span style="color: #660033;">-jar</span> <span style="color: #007800;">$JARFILE</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div>

<p>Jika file <strong>server.pid</strong> tidak ada maka aplikasi servernya akan dijalankan, dalam hal ini berarti aplikasi java. Parameter <strong>&#038;</strong> digunakan untuk mengirim process aplikasi tersebut ke background. Kenapa dikirim ke background? Agar pid aplikasi tersebut dapat diambil menggunakan kode berikut:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PID</span>=<span style="color: #007800;">$!</span></pre></div></div>

<p>Variabel <strong>$!</strong> digunakan untuk mengambil pid dari background process terakhir, dalam hal ini aplikasi server yang kita jalankan sebelumnya.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$PID</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$PIDFILE</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server started with pid:<span style="color: #007800;">$PID</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Setelah itu nilai pid tersebut disimpan ke dalam file, dan skrip akan menampilkan pesan bahwa server sudah dijalankan dengan pid yang didapatkan sebelumnya.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> stopServer<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;">#check if server already running</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDFILE</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span></pre></div></div>

<p>Seperti sebelumnya, skrip akan mengecek apakah servernya berjalan atau tidak dengan melihat apakah file <strong>server.pid</strong> ada atau tidak, apabila ada</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PID</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$PIDFILE</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server running with pid:<span style="color: #007800;">$PID</span>&quot;</span></pre></div></div>

<p>nilai pid-nya akan disimpan ke variable <strong>$PID</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> <span style="color: #007800;">$PID</span></pre></div></div>

<p>kemudian process tersebut diberhentikan secara paksa menggunakan perintah <strong>kill</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #007800;">$PIDFILE</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$LOG</span> <span style="color: #007800;">$LOG</span>-<span style="color: #007800;">$DTM</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server stopped&quot;</span></pre></div></div>

<p>file <strong>server.pid</strong> kemudian dihapus dan file log yang dihasilkan aplikasi server tersebut di rubah namanya ditambahkan dengan informasi waktu saat perintah ini dijalankan.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server are not running&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Jika tidak ada skrip akan menampilkan pesan bahwa process yang dimaksud tidak ada.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$BASEDIR</span></pre></div></div>

<p>Agar semua proses eksekusi dijalankan dari direktori utama, kita pindah ke direktori yang ditentukan sebagai direktori utama dari skrip server ini.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;start&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span></pre></div></div>

<p>Kemudian dicek apabila parameter <strong>start</strong> yang diberikan maka</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server start command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to start the server here</span>
startServer
<span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<p>skrip akan memanggil fungsi <strong>startServer</strong>. Cara menjalankan fungsi di bash cukup dengan memanggil nama fungsinya seperti diatas.<br />
Jika yang berikan adalah parameter <strong>stop</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;stop&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server stop command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to stop the server here</span>
stopServer
<span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<p>maka panggil fungsi <strong>stopServer</strong>.<br />
Jika parameter yang dipanggil <strong>restart</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">&quot;restart&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;server restart command issued...&quot;</span>
<span style="color: #666666; font-style: italic;">#put the code to restart the server here</span>
stopServer
startServer
<span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<p>Jalankan fungsi <strong>stopServer</strong> terlebih dahulu dilanjutkan dengan <strong>startServer</strong>. Fungsi restart disini tidak akan memeriksa apakah aplikasinya sedang berjalan atau tidak, karena memang tidak ada bedanya.<br />
Jika user memberikan parameter diluar yang sudah kita tentukan diatas</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;unknown command <span style="color: #780078;">`basename $1`</span>, supported command are: start, restart, stop&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>maka skrip akan menampilkan pesan bahwa paramater yang dimaksud tidak dikenal.</p>
<h2>Summary</h2>
<p>Flow dalam scripting language seperti bash cukup mudah untuk dipahami, seperti dalam contoh diatas, skripnya menjalankan baris-baris kodenya satu-persatu mulai dari inisialisasi variabel, pembuatan fungsi sampai flow utamanya yaitu dengan mengecek paramater mana yang dimasukkan user dan menjalankan perintah sesuai dengan parameter yang dimasukkan.<br />
Koreksi, saran dan pertanyaan silakan gunakan fasilitas komentar di bawah ini.</p>
<p>Semoga bermanfaat and have a lot of fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.neuronworks.net/2008/12/29/membuat-application-control-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

