`

在windows里直接运行cygwin脚本

 
阅读更多
cygwin是个很好用的东西,问题是,我们想让系统自动化启动一些东西,
比如在windows启动时,调用cygwin的脚本、启动cygwin里的nginx,启动
windows的MySQL数据库等。

这时就需要通过windows直接运行cygwin里的脚本或命令了

有些哥们确把很多事都想好了。

以下第一种方案经测试可行,可以直接启动nginx,执行脚本。

方案一
D:\cygwin64\bin\run bash --login -c  /home/someuser/shell/runs.sh



Running a shell Script Directly from windows
引用


Running a shell Script Directly from windows

    From: Fred Kulack <kulack at us dot ibm dot com>
    To: cygwin at cygwin dot com
    Date: Thu, 8 Jul 2004 11:14:28 -0500
    Subject: Re: Running a shell Script Directly from windows

I have had the best luck using the 'run' utility to avoid extra
consoles and other 'shtuff' like that.
Haven't seen any doc about it though so it may disappear.
Dunno the details.

For example, I create a shortcut for running X so that I don't
get the extra console window where bash ran...

That shortcut runs this:
C:\cygwin\usr\X11R6\bin\run.exe bash --login -c startx

Clearly your path needs to be setup correctly.


"The stuff we call "software" is not like anything that human society
  is used to thinking about. Software is something like a machine, and
  something like mathematics, and something like language, and
  something like thought, and art, and information...
  but software is not in fact any of those other things."
Bruce Sterling - The Hacker Crackdown

Fred A. Kulack - IBM eServer iSeries - Enterprise Application Solutions
ERP, Java DB2 access, Jdbc, JTA, etc...
IBM in Rochester, MN  (Phone: 507.253.5982   T/L 553-5982)
mailto:kulack/us.ibm.com   Personal: mailto:kulack/magnaspeed.net
AIM Home:FKulack  AIM Work:FKulackWrk
MSN Work: fakulack/hotmail.com (replace email / with @)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/





方案二
Running a shell Script Directly from windows
引用

Re: Running a shell Script Directly from windows

    From: Brian Dessent <brian at dessent dot net>
    To: cygwin at cygwin dot com
    Date: Wed, 07 Jul 2004 19:32:24 -0700
    Subject: Re: Running a shell Script Directly from windows
    Organization: My own little world...
    References: <Pine.LNX.4.44.0407072102550.1460-100000@ccc3.wpi.edu>
    Reply-to: cygwin at cygwin dot com

Scott Emerson Longley wrote:

> I am wondering if Cygwin provides a way to run shell scripts or other
> programs that run within Cygwin, directly from windows (or a .bat). In
> other words, I would like to double-click something on my desktop and have
> it run the shell script. I have fooled a little with bash command-line
> options and whatnot, but to no avail. Any insight or URL's that lead to an
> answer would be greatly appreciated.

Create a shortcut that runs "sh.exe /path/to/script.sh".  When you click
on it your script will run in a command window.  If your cygwin bin
directory is not in your path then the shortcut will have to include it,
i.e. "c:\cygwin\bin\sh.exe /home/foo/bar.sh".  The executable's location
is a Windows path, the args to it are POSIX paths.  If the script has
something other than /bin/sh in the shebang, then substitute
appropriately.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/





rem window调用cygwin脚本
@echo off

if  "%1" neq "stop" ( goto startSys  ) else ( goto stopSys  )

:startSys
echo 启动系统。。。
D:\cygwin64\bin\run bash --login -c  /home/someone/shell/runs.sh -wait
goto end

:stopSys
echo 停止系统。。。
D:\cygwin64\bin\run bash --login -c  /home/someone/shell/stop.sh -wait
goto end

:end

echo 执行完成。。。



通过cygwin脚本启动和停止MySQL、tomcat、nginx

#!/bin/bash
#/home/someuser/shell/runs.sh

if  [ "$1" != "stop" ];  then
	echo start now . . .
	# start msyql
	echo start mysql  . . .
	/cygdrive/d/mysql-5.6.10-winx64/bin/mysqld &
	
	#start tomcat
	echo start tomcat7  . . .
	net start "tomcat7"
	
	# start nginx
	echo start nginx  . . .
	/usr/local/nginx/sbin/nginx.exe
	
else

	echo stop now . . .
	# stop msyql
	echo stop mysql  . . .
	/cygdrive/d/mysql-5.6.10-winx64/bin/mysqladmin -uroot -proot shutdown &
	
	#stop tomcat
	echo stop tomcat7 . . .
	net stop "tomcat7"
	
	# stop nginx
	echo stop nginx . . .
	/usr/local/nginx/sbin/nginx.exe -s stop


fi

echo done . . . 





Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台:

         /usr/local/mysql/bin/mysqld_safe --user=mysql &

 但是我们很多程序并不象mysqld一样可以做成守护进程,可能我们的程序只是普通程序而已,一般这种程序即使使用 & 结尾,如果终端关闭,那么程序也会被关闭。为了能够后台运行,我们需要使用nohup这个命令,比如我们有个start.sh需要在后台运行,并且希望在后台能够一直运行,那么就使用nohup:

            nohup /root/start.sh &

          在shell中回车后提示:
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics