Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7458
  • Last Modified:

Bash Scripts to run program

Hi all I am tring to find or get help to wright bash script to run a program at night and the stop
I need the program to start running at 10pm and stop at 6am which i was going to run via cron I have never coded a bash script so i am lost here.

The program script lives in the home directory and needs to be excuted in directory it lives in
the program is web crawler it crawls UK sites only so I want to pass one command to the crawler between hours 10pm to 6am   then the it stops then i wnat to pass another command to the crawler between the hours 7am to 9pm. then set up with cron.

I have tried this with cron but it does not work
cd /home/user/program_dir/ ./myprogram -a -c test
0
Darrennew
Asked:
Darrennew
  • 3
  • 3
  • 2
  • +2
3 Solutions
 
cuziyqCommented:
You can write bash scripts just like you would an MS-DOS batch file.  Simply put a #! on the first line, and bash will know it's a script.  Write however many you want with the commands you would normally just type in at the command line.  Then, add those scripts to cron.  It won't care that they're scripts and not binaries.
0
 
ozoCommented:
0 22 * * *    cd /home/user/program_dir/ ; ./myprogram -a -c test
0
 
TintinCommented:
cd /home/user/program_dir/ ./myprogram -a -c test

should be

cd /home/user/program_dir/; ./myprogram -a -c test

But why not just

/home/user/program_dir/myprogram -a -c test

Just to make cuziyg's comment clearer, bash scripts should start with

#!/bin/bash

Putting #! by itself does nothing.  It just runs the script it whatever shell you are running at the time.  Certainly not a good idea.

Anyway, for the cronjob setup, how often do you want to run the script?  Say it's every half hour, then if you type in

crontab -e

and add the following entries:

0 22-23,0-6 * * * /home/user/program_dir/myprogram -a -c parameter1
0 7-21 * * * /home/user/program_dir/myprogram -a -c parameter2
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:
Where I say "every half hour" above, I meant "every hour"
0
 
DarrennewAuthor Commented:
Tintin

Your's sounds what i am after a know about bash #! I need to run crawler every day starting from 10pm to 6am then stop the crawler but i don't just want to kill the crawler at 6am as it writes to mysql database and that would not be good i need just to issue stop command to to crawler so the crawler itself will stop and write all threads to the database before stopping.  Then issue another command at 7am to 9pm to the crawler to run am crawl which uses less threads and will not over web servers then stop this am crawl and start all over again every day?.

this what i have got so far with custom command
------------------
PM Crawl
./MYPROGRAM -i google.co.uk -u -l 100 -x & sleep 600 kill -0 $! && kill $!

AM Crawl with crawl delay of 1 second & only using 10 Treads
./MYPROGRAM -i google.co.uk -u -l 100 -x -d 1 -t 10 & sleep 600 kill -0 $! && kill $!

the 600 should = 10 Hours in seconds but the kill is killing all crawler threads instantly.

Many Thanks
Darren
0
 
ozoCommented:
10 hours is 36000 seconds
what is the stop command that you need to issue?
0
 
DarrennewAuthor Commented:
HI ozo

The command via key board is alt&c to stop the crawler then the crawler stops after all threads have finished?

Cheers
Darren
0
 
ozoCommented:
if you mean <control-c> that probably sends a kiil -2
0
 
DarrennewAuthor Commented:
Hi Sorry yes you are right ctrl&c ?

ozo me being idot some times would i put all this in bash script and then run script via cron or just put it in directly as cron job?

Does this look correct to you ozo

cron start crawler at 10pm every day
PM Crawl

 cd /home/user/crawler/; ./MYPROGRAM -i google.co.uk -u -l 100 -x & sleep 2800 kill -2 $! && kill $!

cron start crawler at 7am every day
AM Crawl with crawl delay of 1 second & only using 10 Treads

cd /home/user/crawler/; ./MYPROGRAM -i google.co.uk -u -l 100 -x -d 1 -t 10 & sleep 15400 kill -2 $! && kill $!

many thanks
Darren
0
 
omarfaridCommented:
Hi,

If I got your requirement correctly, then

- Have a script to start the morning job, call it start_morning_job

- Have a script to stop the morning job, call it stop_morning_job

- Have a script to start the evening  job, call it start_evening_job

- Have a script to stop the evening job, call it stop_evening_job


In the above start scripts, put all commands necessary to start the crawler (including env variables settings, changing to your home dir, etc.).

For stopping scripts, if you find that kill -2 command is doing the proper shutdown, then you may put the following in them:

kill -2 `ps -ef | grep MYPROGRAM | awk '{print $2}'`

- Make all the above scripts executable

chmod u+x start_morning_job stop_morning_job start_evening_job stop_evening_job

- create a file called my_cron (using editor like vi)

0 7 * * * /path/to/myhomedir/start_morning_job
0 21 * * * /path/to/myhomedir/stop_morning_job
0 22 * * * /path/to/myhomedir/start_evening_job
0 6 * * * /path/to/myhomedir/stop_evening_job

- submit your cron jobs:

crontab my_cron

- To cerify, run

crontab -l



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now