View previous topic :: View next topic |
Author |
Message |
niel6b New Member
Joined: 14 Dec 2025 Posts: 3
|
Posted: Sun Dec 14, 2025 10:50 pm Post subject: test a condition and then run a command via crontab |
|
|
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 |
|
crouse Site Admin

Joined: 17 Apr 2025 Posts: 11833 Location: Iowa
|
Posted: Sun Dec 14, 2025 11:12 pm Post subject: |
|
|
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 |
|
niel6b New Member
Joined: 14 Dec 2025 Posts: 3
|
Posted: Mon Dec 15, 2025 12:04 am Post subject: |
|
|
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 |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Mon Dec 15, 2025 1:56 am Post subject: Some possible options |
|
|
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 |
|
|
Back to top |
|
niel6b New Member
Joined: 14 Dec 2025 Posts: 3
|
Posted: Mon Dec 15, 2025 3:18 am Post subject: |
|
|
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 |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Mon Dec 15, 2025 3:47 am Post subject: Check access |
|
|
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. |
|
Back to top |
|
|