USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

test a condition and then run a command via crontab

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
niel6b
New Member


Joined: 14 Dec 2024
Posts: 3

PostPosted: Sun Dec 14, 2024 10:50 pm    Post subject: test a condition and then run a command via crontab Reply with quote

I run following command using Freenet:

cd ~/Freenet
./run.sh status
Then I get either of the following text messages:
Freenet 0.7 is not running
or
Freenet 0.7 is running (5497) #this last number changes from time to time

If Freenet is not running I start it by the following command:
cd ~/Freenet
./run.sh start

I need a bash script, which starts Freenet if Freenet is not running, and do nothing if Freenet is running

I would call the script: ~/freenet_status_test.sh

Then I would make a crontab file called ~/.crontab
with the following contents:
0,10,20,30,40,50 * * * * user1 ~/freenet_status_test.sh
#This should test every 10 minutes if Freenet is running

Thanks in advance


Back to top
View user's profile Send private message
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Sun Dec 14, 2024 11:12 pm    Post subject: Reply with quote

It would definately be just as easy to modify the first two scripts and creating one......... but I'd have to see those before hand. Welcome to usalug.org



_________________
Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
Back to top
View user's profile Send private message Visit poster's website AIM Address
niel6b
New Member


Joined: 14 Dec 2024
Posts: 3

PostPosted: Mon Dec 15, 2024 12:04 am    Post subject: Reply with quote

It is not any scripts, I am just telling what I do from the command line.
I need a script with some kind of an if test.
I have tried to make a script, but it do not seem to work:

***

#!/bin/bash
cd ~/Freenet
./run.sh status > ~/freenet_test.txt #Places the output of the "./run.sh status" command in the ~/freenet_test.txt file
if [[ `cat ~/freenet_test.txt | grep -i "is not running"` != "" ]] #grep finds any instances of "is not running" in the ~/freenet_test.txt file
then
#whatever commands you wanted to run bc of this error namely
./run.sh start #starts freenet
fi

***


Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Mon Dec 15, 2024 1:56 am    Post subject: Some possible options Reply with quote

What happens when you attempt to run this script?

What is in the file ~/freenet_test.txt?

Is this script executable?

You may also be able to detect the presence of freenet by looking in the process status table, depending on the name of the process that runs. Not sure if that would be a reliable means of verifying the health of the application, but it may identify its presence, e.g., something like:


Code:
#!/bin/bash
cd ~/Freenet
freenet_running=`ps ax | grep freenet | grep -v grep`
if [[ ! "$freenet_running" ]]; then
   ./run.sh start #starts freenet
fi


Code:
#!/bin/bash
cd ~/Freenet
freenet_output=`./run.sh status`
if [[ `echo "$freenet_output" | grep -i "is not running"`]]; then
  ./run.sh start #starts freenet
fi



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
niel6b
New Member


Joined: 14 Dec 2024
Posts: 3

PostPosted: Mon Dec 15, 2024 3:18 am    Post subject: Reply with quote

I just tried to run the script when Freenet was running, and the ~/freenet_test.txt file contained:

Freenet 0.7 is running

Then I stopped Freenet, and ran the script, and the ~/freenet_test.txt file then contained:

Freenet 0.7 is not running

(and by the way when I then tried to start Freenet from the command line it said that Freenet was already running) - so it looks like my script is working, but for some reason it did not start, when run via ~/.crontab.

My problem is that I am testing a small fit-pc with only 256 mb of ram, running Freenet, and it runs for hours, and then stops, but starts immediately I start it from the command line, so I just try to overcome the problem by using the script, which I found on bashscripts.org, and then adapted it for my purpose, as shown in my script.


Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Mon Dec 15, 2024 3:47 am    Post subject: Check access Reply with quote

Make sure that the script can be accessed from the cron job - the path needs to be correct and the file access rights need to be sufficient to be able to run the script. Either one of those conditions could create issues if not verified. To be safe, specify the full path to the script and make the script world readable and executable, then it ought to work, assuming that all else is correct.



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse