Cause:
I often need to check the status of some Windows Services. Every time I use the control panel> Service to view the Service status, it is not only a waste of time but also boring. Simply write a Ruby script. Let the Ruby script notify me of the status of a Windows Service and give me some prompts Based on the status.
Main content:
You can see the use of the following technologies/tools in this article daily works with ruby.
- Use RubyGems to query and install the Gem package.
- Query the Windows service status in Ruby.
- Call Windows API in Ruby.
1. Install RubyGems
1. You can go to RubyForge to download the latest RubyGems. RubyGems is a package management tool used in Ruby. After RubyGems is installed, you can run the gem command to query and install other Ruby libraries. Download the zip file of the RubyGems installation package and decompress it to the Directory D: \ Temp. Run the following command to install RubyGems:
C:\Documents and Settings\Jerry>d:D:\>cd Temp\rubygems-1.3.1D:\Temp\rubygems-1.3.1>setup.rbmkdir -p C:/Ruby/lib/ruby/site_ruby/1.8mkdir -p C:/Ruby/bin
After the installation is complete, you can use RubyGems to query and install the Ruby gem package.
2. Install the win32-service package
1. Query
After installing the RubyGems Package Manager, we can use the gem command to query and install the ruby gem package. When we Access Windows Services, we need to use the wind32-service package, and now we have typed the following command in the command line to query:
1 D: \ Temp \ rubygems-1.3.1> gem query-r-n service
2
3 *** remote gems ***
4
5 actionservice (0.3.0)
6 actionwebservice (1.2.6)
7 ar_mailer_service (0.1.1)
8 contxtlservice (0.1.1)
9 dot_net_services (0.4.0, 0.3.0)
10 mongrel_service (0.3.4, 0.1)
11 ruby_service_helper (0.1.0)
12 servicemerchant (0.1.0)
13 win32-service (0.7.0)
14 win32_service_manager (0.1.3)
15
16 D: \ Temp \ rubygems-1.3.1>
The gem query command is used to query available gem packages. You can use gem -- help to obtain more available options.
The gem query-r-n service indicates to go to the [-r option] of the remote server and find the gem package named [-n option] as service.
The 13 lines above show the package we need, that is, the win32-service.
2, install win32-service
Execute the following command to install the win32-service package:
1 D: \ Temp \ rubygems-1.3.1> gem install-r win32-service
2 Successfully installed win32-service-0.7.0-x86-mswin32-60
3 1 gem installed
4 Installing ri documentation for win32-service-0.7.0-x86-mswin32-60
5 Installing RDoc documentation for win32-service-0.7.0-x86-mswin32-60
6
7D: \ Temp \ rubygems-1.3.1>
After installing the win32-service, we can view the documentation and API descriptions for the win32-service.
Run the following command from the command line:
1 D: \ Soft \ AutoHotkey104805> gem server
2 Starting gem server on http: // localhost: 8808/
Then we can open a browser to access http: // localhost: 8808/, and then we can access the Documents of all the Gem packages.
3. Compile a script to query the Windows Service Status
Everything is ready. We started to write scripts.
#!/ruby/binrequire "win32/service"require "Win32API"include Win32if Service.exists?("WebClient") && Service.status("WebClient").current_state == "running" beep = Win32API.new('kernel32','Beep',['I']*2,'V') 5.times do beep.call(750,300) sleep(1) endend
Here we need to explain the following lines: beep = Win32API. new ('kernel32', 'beep ', [' I '] * 2, 'V ')
Here we declare a Windows API. The Win32API. new method has four parameters.
1. Name of the dynamic link library of the API, such as user32 and kernel32 ....
2. API method name.
3. input parameter type: Two Integer Parameters. Commonly Used parameters include ['P']-Pointer, ['V']-void, ['n']-number.
4. Return Value Type.
Iv. Conclusion
It is convenient to use RubyGems to query Ruby libraries. It is also easy to use Ruby modules to call Windows APIs to complete specific tasks.
If you are not familiar with some APIs, you can use the "gem server" command to open the local www Service to view the corresponding Gem documentation.