Windows Run a Windows Application as a Service with srvany

0 Comments

Occasionally the need arises for running an application in Windows as a service. This allows the application to run at boot time without the need for a user to be logged into the system in order for the application to start and operate. While built-in mechanisms such as Task Scheduler exist to help facilitate this, the ability to run an application as a service has certain advantages, notably the ability to prescribe certain actions be taken should the service fail. Take for example the the case where an application is listening for incoming IP connections. Being able to automatically restart the service without user intervention is desirable in order to avoid loss of service.

This post will describe how to use the Microsoft utilities instrsrv.exe and srvany.exe to install a Windows application as a service. instrsrv.exe is used to install the service while srvany.exe acts as a wrapper around the application and handles the service events. Both utilities are available as part of Microsoft’s Windows Server 2003 Resource Kit Tools. I have successful tested instrsrv.exe and srvany.exe on Windows 7/8.1; I have not tested them on Windows 10.

Let’s get started…

For purposes of example, we’ll assume we have a Windows application called “foobar” that is normally started by using the binary c:\foo\foobar.exe.

Start by downloading Microsoft’s Windows Server 2003 Resource Kit Tools and install it to a folder of your choice. Create a folder to contain the files instrsrv.exe and srvany.exe. We’ll use c:\srvany for our example. Now copy both files from the where you installed the Resource Kit tools to c:\srvany. After the files have been copied the Windows Server 2003 Resource Kit Tools may be uninstalled.

Now open a command prompt and install foobar as a system service using the following command. You may use any name you’d like for the service:

You should receive a response indicating that the service was successfully added.

Next, open the Windows registry editor and navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\foobar. Use the mouse or keyboard to highlight foobar and select Edit->New->Key to create a new key named Parameters. Highlight Paramters and select Edit->New->String Value to create a new string value named Application. Double-click on Application and enter the full path to foobar.exe (e.g., c:\foo\foobar.exe). That’s it. You can now close the registry editor.

By default, the newly created service foobar is configured to run automatically when the system is restarted. To change this setting to Manual, run the Services applet by entering services.msc from a command prompt or by using WIN+R. Locate foobar from among the list of services, right-click on it and select Properties and change its Startup type to Manual. A service set to Manual can be started from within the Services applet, or by entering the following from a command prompt:

Selecting the Recovery tab from within properties will allow you to chose what the system should do in the event if the service fails.

You can delete the service by entering the following from a command prompt:

There you have it. A nice simple way to register and run a non-service application as a windows service.

Tags: ,

News Changing Code Syntax Highlighters

0 Comments

Most of the posts I author here contain code examples. To highlight these code examples I’ve been using the wordpress plugin SyntaxHighlighter Evolved. For the most part this plugin has served me, and I hope you, well. There are a few quirks, however, that continue to annoy me about this plugin. Chief among those quirks is the need to “minify” its Javascript and CSS following each update to maintain rendering performance. That and its lingering use of the Adobe Flash file format SWF for its copy-to-clipboard feature, even though that feature was deprecated.

So I’m making a change. I’m dropping SyntaxHighlighter Evolved for Crayon Syntax Highlighter, a nice syntax highlighter built in PHP and jQuery. It’s intuitive to administer and provides some welcome relief from those aforementioned annoyances. More importantly though it offers you some nice features. For example mousing over the highlighted code results in a drop-down box where you can toggle line numbers on/off (they’ll be on by default), toggle line wrapping on/off (longer lines will wrap by default), view plain code, copy the code, or view the code in a separate browser window. You can try these features yourself in the following example:

In the next week or so I’ll begin transitioning older posts containing code examples over to this new plugin, and of course new post will use this plugin by default. Enjoy, and please let me know if you encounter any problems.

Tags:

Networking How to Install and Configure MRTG on FreeBSD

0 Comments

In a previous post I described how to install and configure Tobi Oetiker’s MRTG (Multi Router Traffic Grapher) on a Ubuntu server. In this post I will describe how to install and configure it on FreeBSD. Once configured, you’ll be able to use MRTG to monitor the traffic in and out of your network using the SNMP capability in your network’s gateway/router. MRTG generates static HTML pages containing PNG images which provide a visual representation of this traffic. MRTG typically produces daily, weekly, monthly, and yearly graphs. MRTG is written in perl and works on Unix/Linux as well as Windows. MRTG is free software licensed under the GNU GPL.

Software versions used in this post were as follows:

  • apache24 2.4.10_2
  • FreeBSD 10.2-RELEASE
  • mrtg-2.17.4
  • The steps discussed assume that the FreeBSD Ports Collection is installed. If not, you can install it using the following command:

    If the Ports Collection is already installed, make sure to update it:

    Okay, let’s get started. All commands are issued as user root. When building the various ports you should accept the default configuration options.

    Install a http server

    MRTG requires an http server to be installed and operating correctly. In our example, we’ll install and use the Apache http server. Navigate to the Apache port and build it:

    Once Apache has been successfully installed, add the following line to /etc/rc.conf so that the Apache server will start automatically at system boot.

    Now let’s start Apache to make sure it works:

    Point your web browser to the host name or IP address of the FreeBSD host you’ve installed Apache on and you should see the venerable “It works!”

    Install and configure MRTG

    Now that we have an http server up and running let’s install MRTG:

    What does the MRTG port install and where is that stuff located?

    MRTG provides the example configuration file /usr/local/etc/mrtg/mrtg.cfg.sample that describes global configuration parameters as well as various configuration options for the SNMP targets you want to monitor. If you already have some experience with MRTG and SNMP you can simply copy or move this file to /usr/local/etc/mrtg/mrtg.cfg then modify it to meet your requirements. In our example, however, we’re going to create the requisite mrtg.cfg file from scratch.

    MRTG includes the script cfgmaker that will create and populate a basic mrtg.cfg file with information obtained from your gateway/router. So, before running /usr/local/bin/cfgmaker, you should activiate and configure the SNMP service in your gateway/router. This typically involves logging into the device and enabling SNMP. The default SNMP community name is typically “public.” If you change the SNMP community name to something else, make note of it. Now, let’s run cfgmaker, substituting your SNMP community name if you’ve changed it, and adding the IP address of your gateway/router:

    If you would like to add more than one device to mrtg.cfg simply append the additional URL(s) to the same mrtg.cfg file. Then, when you build the web page using the indexmaker command described below, graphs associated with each device will be displayed on the same HTML page:

    Next, open /usr/local/etc/mrtg/mrtg.cfg and, under Global Config Options, uncomment the line WorkDir: /home/http/mrtg and change it to WorkDir: /usr/local/www/apache24/data/mrtg. This is the directory from which the Apache http server will server the MRTG html pages. If you’re using something other than Apache as your http server then you’ll need to change this path.

    Next, uncomment the line Options[_]: growright, bits. By default MRTG graphs grow to the left, so the option growright specifies that the direction of the traffic visible in MRTG’s graphs flips causing the current time to be at the right edge of the graph and the history values to the left. The option bits specifies that the monitored traffic values obtained from your device is multiplied by 8 and displayed bits per second instead of bytes per second.

    MRTG includes the script indexmaker. This is what we’ll use to create the pages used to display the MRTG graphs. First, let’s create the directory from which Apache http server will serve up the pages:

    Then use indexmaker combined with our mrtg.cfg file to create and populate an index.html file in that directory:

    Now we need to add an Alias and a Directory directive to Apache’s configuration file to support MRTG. Open /usr/local/etc/apache24/httpd.conf and add the following lines in the section containing similar Directory directives, or it can simply be appended to the bottom of the file:

    And change the user and group for the following directories to mrtg:

    Finally, let’s restart the http server:

    Starting MRTG

    Okay, now that MRTG has been installed and configured let’s start it up and see what it displays. Add the following line to /etc/rc.conf:

    Then start the MRTG daemon:

    The MRTG daemon will now run automatically each time FreeBSD starts.

    Now point your browser to http://your-http-server-address/mrtg and you should see a page that resembles Figure 1. You may have more or less graphs depending on the number of interfaces reported by your devices(s).

    Screenshot showing the web page generated by MRTG

    Figure 1

    You’ll see the graph starting to “grow” to the right as the traffic is monitored over time, and the Y axis displayed as bits per second. If you click on any one of these graphs you’ll be taken to differnt page showing individual graphs for 30 minute, two hour, and daily averages, along with the maximum, average, and current bit rate in and out of that particular interface. By default, these graphs will update every 5 minutes.

    Only interested in displaying one particular interface? Want to graph other SNMP data? Now that you that you have a basic mrtg.cfg file created you can modify it or incorporate some of the global and target parameter examples contained in the file /usr/local/etc/mrtg/mrtg.cfg.sample to further customize your configuration. Just remember to run indexmaker again to update the MRTG index.html file.

    Conclusion

    This concludes the post on how to install and configure MRTG on FreeBSD. As you can see, MRTG isn’t terribly complicated and proves to be a really nice port for monitoring and graphing traffic in and out your gateway/router. For a full list of all the configuration options and other information I encourage you to visit the MRTG web site.

    References
    http://oss.oetiker.ch/mrtg/doc/index.en.html

    Tags: ,

    BSD Using Tarsnap in FreeBSD to Improve My Offsite Backups

    1 Comment

    In a recent post I described how I improved the reliability of my file system backups by using the data replication capabilities inherent in the FreeBSD Zettabyte File System (ZFS). In this post I will explore Tarsnap, another tool I recently started to use to perform secure offsite backups of my most important files.

    The versions for the software used in this post are as follows:

  • FreeBSD 10.1-RELEASE (running as a guest OS under VMWare ESXi hypervisor 6.0.0)
  • tarsnap 1.0.35_2
  • All commands in this post are issued as user root. The steps discussed assume that the FreeBSD Ports Collection is installed. If not, you can install it using the following command:

    Okay, let’s get started.

    Create a Tarsnap account

    Before installing Tarsnap I visited the Tarsnap registration page and created an account. Tarsnap operates on a prepaid basis, so you have to add some money to your account before you can start using it. The minimum amount is $5.00. Money will be deducted from your pre-paid amount based on the actual number of bytes stored and bandwidth used (after compression and data deduplication). Tarsnap prices are currently $.25 per Gigabyte per month for storage, and $.25 per byte for bandwidth.

    Install Tarsnap

    After creating an account it was time to install Tarsnap. First I made sure the Ports Collection was up to date:

    Then proceeded with the install, accepting all default configuration options:

    Next, I ran tarsnap-keygen, a utility which registers my machine with the Tarsnap server and generates a key that is used to encrypt and sign the archives that I create. I needed to have the e-mail address and password I used to create my Tarsnap account handy when running this command. In following example I’ve registered a machine with the host name tarsnap-test:

    Note that if I had multiple machines containing files I wished to backup to Tarsnap, I would want create a separate key file for each machine.

    By default tarsnap-keygen will create the key file /root/tarsnap.key. This can be changed by adding the option keyfile to specify a different location and/or key name. In the example above I’ve changed the name of my key file to tarsnap-test.key to help disambiguate keys in case I add additional machines to my Tarsnap account in the future.

    Tarsnap creates the file /usr/local/etc/tarsnap.conf when installed. This config file is read by the tarsnap utility and specifies a number of default options, all of which will be ignored if the options in question are specified at the command line. Since I change the name of default key file, I revised the value for the option keyfile in /usr/local/etc/tarsnap.conf:

    Note that you should store a copy of this key someplace safe. If you lose your Tarsnap key file(s), you will not be able to create new archives or access your archived data.

    Using Tarsnap

    After installing Tarsnap I was ready to create and backup my first archive. Tarsnap commands follow a syntax similar to the venerable tar utility. The -c option creates a new archive containing the specified files. The -f option specifies which file to write the archive to:

    Performing subsequent backups of these files will go faster since Tarsnap’s deduplication feature will avoid sending data which was previously stored.

    If I want to list all archives stored with Tarsnap I can use the following command:

    Adding one or more instance of the -v option to this command will make the output more verbose. For example, if -v is specified one or more times, the creation time of each archive is printed; if it is specified two or more times, the command line with which Tarsnap was invoked to create each archive is also printed.

    If I want to list the files contained within a single archive I can use the following command:

    The -t option is used to print to stdout the files stored in the specified archive; the -v option of course makes the output a little more verbose.

    If I wanted to delete one or more archives I can use the -d option:

    When the time comes to restore one or more files from Tarsnap I have a couple of options. For example, I can recover all files contained in a particular archive using the following command. In this example, I’ve extracted all files contained in the archive backup-20150729 to /tmp where I can recover one of more files:

    Or I can extract just one of the directories in this archive:

    Note here that you must exclude the leading / from the directory you’re restoring from. So in this case, instead of /pool_0/dataset_0/some-directory/, it should be pool_0/dataset_0/backup/some-directory.

    Or regress even further into the archive to recover a single file if desired:

    Finally, if for whatever reason I no longer wish to use Tarsnap on this machine I can invoke the nuke option, which will delete all of the archives stored:

    To make sure you’re really serious, Tarsnap will ask you to type the text “No Tomorrow” when using this command.

    Okay, after getting comfortable with the Tarsnap commands and backing up files manually for a couple of days, I created this ugly little script that creates a daily archive of a specified directory; looks for any archives older than 30 days and deletes them; and, logs its output to the file /home/iceflatline/cronlog:

    I wrote the script to /home/iceflatline/bin/tarsnap.sh where I maintain some other scipts and made it executable:

    Then added the following cron job to the crontab under user root. The script runs every day at 0800 local time:

    Conclusion
    Well, that’s it. A short post describing my experiences using Tarsnap, an easy, secure and inexpensive solution for performing offsite backups of my most important files.

    References
    https://www.tarsnap.com/gettingstarted.html
    https://www.tarsnap.com/man-tarsnap.1.html

    Tags: , ,

    BSD Replacing MySQL with MariaDB in FreeBSD

    1 Comment

    In my post on how to install and configure Apache, MySQL, PHP and phpMyAdmin on FreeBSD (FAMP) for basic local web development activities, one of the components is the MySQL database server. But what if you prefer to use MariaDB?

    MariaDB is an open source alternative to MySQL, and available under the terms of the GNU GPL v2 license. It is developed by the MariaDB community with oversight by the MariaDB Foundation. For all practical purposes MariaDB is a drop-in replacement for the same MySQL version. All commands, interfaces, libraries and APIs that exist in MySQL also exist in MariaDB. For example, MySQL 5.1 and MariaDB 5.1 are compatible, as are MySQL 5.5 and MariaDB 5.5. MariaDB 10.0 is the drop-in replacement for MySQL 5.6, and can also replace MySQL 5.5.

    This post will use the aforementioned post on how to install and configure Apache, MySQL, PHP and phpMyAdmin on FreeBSD as an example implementation and demonstrate how to install and configure MariaDB as a replacement for MySQL. I strongly encourage you to test these steps first before using them on your development or production environment. At the very least you should backup your database(s).

    The versions of software discussed in this post are as follows:

  • FreeBSD 10.1-RELEASE (running as a guest OS under VMWare ESXi hypervisor 6.0.0)
  • apache24 2.4.12
  • mysql56-server 5.6.24
  • mariadb55-server 5.5.43
  • mariadb100-server 10.0.17
  • mod_php56 5.6.11
  • php56 5.6.10
  • php56-extensions 1.0
  • The following steps discussed in this post assume you have the FreeBSD Ports Collection installed. If not, you can install it using the following commands:

    If the Ports Collection is already installed, make sure to update it:

    Okay, let’s get started. All commands are issued as the user root. While building the various ports you should accept all default configuration options unless otherwise instructed.

    Building FAMP with MariaDB

    To add MariaDB 10.0 when building a new FAMP implementation, use the following commands:

    Then add the following line to /etc/rc.conf:

    Start the MariaDB server:

    Then create a password for the MySQL root user:

    If you’d like to add MariaDB 5.5 instead of MariaDB 10.0 when building a new FAMP implementation, use the following commands:

    Then use the commands described previously to add MariaDB to /etc/rc.conf, start the server, and create the root password.

    Replacing MySQL with MariaDB in an existing FAMP implementation

    If you’ve previously built FAMP using the MySQL 5.6 server, then you can replace it with MariaDB 10.0. First, make sure to backup your database(s), then uninstall the MySQL 5.6 server and client:

    Install MariaDB 10.0:

    Then finish by running the command mysql_upgrade. This command does two things: it ensures that your mysql privilege and event tables are updated with the new fields MariaDB uses; and, it performs a check of all tables and marks them as compatible with MariaDB 10.0. In most cases this should be a fast operation (depending on the number of database tables):

    If you’re currently using MySQL 5.5, then you can replace it with either MariaDB 5.5 or MariaDB 10.0. To replace it with MariaDB 5.5:

    While not strictly necessary in this case, you should also run mysql_upgrade. If calling mysql_upgrade was not necessary, it does nothing.

    Conclusion

    Well, that’s it. A few minutes of your time with the FreeBSD Ports Collection and you can quickly replace MySQL with the open source alternative MariaDB on your FAMP implementation.

    References

    https://mariadb.com/kb/en/
    https://mariadb.com/kb/en/mariadb/what-is-mariadb-55/
    https://mariadb.com/kb/en/mariadb/what-is-mariadb-100/

    Tags: ,