Find the answer to your Linux question:
Results 1 to 7 of 7
Hello! I've been setting up a server for my website, that uses a server version of CentOS. In case you don't know, CentOS is a community-supported free version of Red ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Location
    IceWM
    Posts
    23

    Question Cron question with CentOS...


    Hello!

    I've been setting up a server for my website, that uses a server version of CentOS. In case you don't know, CentOS is a community-supported free version of Red Hat Enterprise Linux.

    I'm wondering if the following is possible. You see, my server runs a IRC bot (which is a java logbot available at jibble.org) to log all the things said in a channel on a IRC network. I boot it up currently via SSH, using PuTTY on my windows PC. Only thing is, when I close down PuTTY, it closes the bot too.

    I'm wondering if it's possible to make a cron job that runs every 2 hours or so, that looks for the java process (as the bot is invoked by java -classpath....) and if it's running, exits, but if it's not running, it starts the java process with the needed parameters.

    Is this possible? I don't want to have clones of my IRC bot starting up every 2 hours. (If so, what would the script be?)

    Thanks in advance.

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,414
    You could write a script that tries to find the process id (using ps or pgrep) of the bot, and, exits if it finds a valid id, or starts the bot if it doesn't.
    Code:
    man ps
    man pgrep
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Location
    IceWM
    Posts
    23
    Okay, sounds interesting. I'll look into it.

    EDIT:
    I ran this:
    Code:
    pgrep java
    ..and got nothing. Okay, good. So then I started the bot via PuTTY. Now, I ran the same command and I got this:
    Code:
    [root@krystalserv ~]# pgrep java
    3497
    [root@krystalserv ~]#
    ...so 3497 is the job ID, is it? Now I have to somehow manage to make a script for cron that starts it (java bot process) up if pgrep doesn't output anything.
    Last edited by Coburn64; 10-01-2008 at 05:42 AM. Reason: More Progress Info.

  4. $spacer_open
    $spacer_close
  5. #4
    Administrator jayd512's Avatar
    Join Date
    Feb 2008
    Location
    Kentucky
    Posts
    5,023
    I've not scheduled cron jobs before, but maybe this page will be of some help.
    Jay

    New users, read this first.
    New Member FAQ
    Registered Linux User #463940
    I do not respond to private messages asking for Linux help. Please keep it on the public boards.

  6. #5
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,414
    I'm not good at writing shell scripts on the fly like this, but you've got the idea.

    Assign the output of "pgrep java" to a variable. Then check the contents of that variable. If it's empty, execute the command to start the java bot, if it's not, then exit the script.

    It should be pretty easy to figure out how to throw that together in Bash (or whatever your favourite scripting environment is).
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  7. #6
    Just Joined!
    Join Date
    Oct 2008
    Location
    IceWM
    Posts
    23
    Quote Originally Posted by smolloy View Post
    I'm not good at writing shell scripts on the fly like this, but you've got the idea.

    Assign the output of "pgrep java" to a variable. Then check the contents of that variable. If it's empty, execute the command to start the java bot, if it's not, then exit the script.

    It should be pretty easy to figure out how to throw that together in Bash (or whatever your favourite scripting environment is).
    Ah, I was experimenting with PHP and running the bot via there (exec commands and such). If $logbotrun was empty, start it (java command and such), if the variable had something in it (process id), the script exited.

    Only thing that it did was on the exec('java ...'); command, it started the javabot, but php's command line interface just hung there! Oh wells, I'll see how this works. Thanks again!

  8. #7
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,414
    I have no idea about PHP, but it sounds like you're almost there
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •