Write a shell script file named start.sh
#! Server_name ="ccn.paas.service" # Server_path ="nohup java-jar - Djava. Ext dirs = $JAVA_HOME/jre/lib/ext:. /,. / lib:. / plugins CCN. The paas. Service. Jar > / dev/null # 10240 & "boot method start () {# output - start - echo "-- the start -" # $server_path reference just defined service start running parameters ($server_path) & # found to start the process of pid assigned to pid pid = $(ps - ef | grep $server_name | grep -v grep | awk '{print $2}') # judgment is pid has a value, is whether the service start if [! $pid]; Service is not exit... echo "service is not exit ..." Else # or output pid values such as pid = 12345 echo "pid = $pid # close if judgment" fi} stop () {pid = $(ps - ef | grep $server_name | grep -v grep | awk '{print $2}') if [ ! $pid ]; then echo "service is not exit ..." else kill -9 $pid fi } restart(){ pid=$(ps -ef|grep $server_name |grep -v grep |awk '{print $2}') if [ ! $pid ]; then echo "service is not exit ..." start else kill -9 $pid start fi } checkrun(){ while true do pid=$(ps -ef|grep $server_name |grep -v grep |awk '{print $2}') if [[ $pid -eq 0 ]]; Then echo "--restart-" start fi sleep 30s done} # Case $1 in # Execute the start method when the parameter is passed in. Start): start; stop): echo "--stop" stop ;; restart): echo "--restart" restart ;; checkrun): echo "--restart" checkrun ;; *): echo "error ..." ;; esac exit 0Copy the code
Sh start Stop./start.sh stop Restart./start restartCopy the code
An error occurs when you write a shell file on Windows and upload it to a Linux server
Error 1:
[root@10-20-31-41 paas2]#./start.sh # [root@10-20-31-41 paas2]# chmod a+x start.shCopy the code
Error 2:
[root@10-20-31-41 paas2]# ./start.sh restart
-bash: ./start.sh: /bin/bash^M: bad interpreter: No such file or directory
Copy the code
- Shell script files written on Windows are in DOS format, that is, the end of each line is marked with \r\n, whereas Unix files are marked with \n. Several ways to check whether a script file is in DOS or Unix format.
Cat -a start.sh The command output shows that the line end of the DOS file is ^M$, and the line end of the Unix file is $2. Od-t x1 start.sh If 0D 0A is displayed, the file is in DOS format. Vi start.sh Open the file and run the set ff command. If the file is in DOS format, fileFormat = DOS is displayed. If the file is in unxi, FileFormat = Unix is displayedCopy the code
Solution 1:
[root@localhost bin]# dos2unix start.sh
dos2unix: converting file start.sh to UNIX format ...
Copy the code
Solution 2:
Vim start.sh :get fileformat # Check the format of this file :set fileformat= Unix # Set the file to Unix :wq # saveCopy the code
Solution 3:
Sed -i "s/\r//" start.sh or sed -i "s/^M//" start.. shCopy the code
After the above problems are solved, it can be started normally