hi,
i found below online

00 11,16 * * * /home/ramesh/bin/incremental-backup

i understand that it does

00 – 0th Minute (Top of the hour)
    11,16 – 11 AM and 4 PM
    * – Every day
    * – Every month
    * – Every day of the week

but what i need to do is
first i need to stop the servervices , then restart it and then go to few other locations and start couple of thing too how would i do it?

00 11,16 * * * /opt/somefolder1/./stop.sh
00 11,16 * * * /opt/somefolder1/./start.sh 
00 11,16 * * * /opt/somefolder2/./start.sh
00 11,16 * * * /opt/somefolder3/start.sh  

would that work "stop.sh" files stop the service "start.sh" start the service
can you please help?

Recommended Answers

All 26 Replies

Hello,

Not exactly. If I understand you correctly you need to do a couple of things in a specific order to run your backups. Stop some service, then run your backup then restart the service. The easiest way is to create a shell script that does everything then call that script from cron. The linux shell is much more powerful and is like a programming language of it's own. Let's say the service you need to stop is called myservice and your backup program is called /home/ramesh/bin/incremental-backup like in your example. The the script to stop the service, run the backup and restart the service is called cron_backup_code and would be something like this:

#!/bin/bash
/bin/service myservice stop
/home/ramesh/bin/incremental-backup
/bin/service myservice start

then you call that script from cron with

00 11,16 * * * /pathtoomyscript/cron_backup_code

In linux the extension is not needed to make a file execute like windows. It does not have to have .sh or .exe to run just have the execute bit set.

Hello,
thank you very much for your response, that's what i needed to do exactly.
but i am running some problems. i don't know if you can help but i wanted to ask.
i have 3 programs, one of them run perfect but other to having problems.
the ones with problems the way i would manually run them is "sh red5.sh &" and after it runs i do ctrl c + d instead of "./red5.sh"
because when I use "./red5.sh" and close the putty then program stops but when i use the "sh red5.sh &" then ctrl c + d putty disappears itself and everything works nice.

any ideas please?

thank you

Hello,

If I understand correctly what you want to happen is to be able to log off the server (close putty) and still have the program program continue to run till completion. There are a couple of options/applications available for this and one built into the system by default. Look up the command nohup which is short for nohangup and you can find out about it by typing:

man nohup

from the shell when logged in via putty.
To use it in your case you would do:

nohup ./red5.sh

you can then close putty and the program will continue to run and sends any output to a file called nohup.out.
Another option which can be very handy, especially if you need to be able to get back to the running application, is called screen and will keep your shell running as a virtual terminal that you can reconnect to. It is kind of like when you RDP to windows server, close RDP and are able to reconnect to the session later. Here is a link to a site with a good discussion on screen:

http://www.manpagez.com/man/1/screen/

i could only do with

nohup sh red5.sh &

and the simply close the putty and everything works

but how would i get that into shell script?

thank you

hi
if i just use the below right on putty and after close it it works, but i cannot put it in a file and run it, it says no nohup. could you please help?

thank you

nohup sh red5.sh &

hi
if i just use the below right on putty and after close it it works, but i cannot put it in a file and run it, it says no nohup. could you please help?

thank you

nohup sh red5.sh &

when i only add the aboive into a shell script and nothing else, it works but when i do /pattomyscript/nohup sh red5.sh &
then it does not work,
i am sorry if i am mixing this things because i have no idea about this

any help is appreciated
thank you

hello,

nohup only works for the shell script you are calling from the command line it does not work inside a shell.

commented: Don't triple post -1
commented: Good advice! +12

this what i did
i created a file called

test

in the same directory where my red5.sh is

then i added these line into it

/mypath/stop-all-services-file
nohup sh red5.sh &

saved the file and made it exetable

then in the putty
i wanted to try the file if it was doing what i needed to do
i did

> ./test

and it worked, first all service stop then red5 started

but when i try cron to run the "test" , it only stop the services and it did not start red5

is that what you were trying to say?

but then how come it worked when i try to run the "test" right on putty?

thank you

can please someone help me? please
thank you

Hello,

Check the crontab error logs to see what happened. It is probably your path. Cron does not execute with your path and does not know where your programs are. Try putting the full path name in front of your program and it will probably work.

/sbin/service mystuff stop
/home/ramesh/red5.sh
/sbin/service mystuff start

in my case, unfortunately in shell red5 start only with nohup sh red5.sh &
i put the shell where the red5 is, if i put the path in front of nohup sh red5.sh & it does not work
so i was hoping that cron would find the shell file and run that shell automatically the times i specified since that shell would work manually.

if i understand correct what you are saying shell also needs the full path

correct ?

Hello,

You need the full path to red5.sh something like:

nohup sh /home/ramesh/red5.sh &

hi again
somehow nohup does not work anymore for me so i found this

screen /home/ramesh/red5.sh

which works in shell but somehow it does not work with cron,i check some other program start-ups and cron does work for them but not for this one, if I run the shell manually it works but somehow not with the cron

any more ideas please?
also how can i check the if there is any error
thank you

Hello,

You can redirect the output (standard IO and error) of the application to a file like this:

/home/ramesh/red5.sh  2>&1 >> /home/ramesh/mylogfile

The purpose of nohup is to allow the shell to keep running when the terminal disconnects. In cron there is no actual terminal so you don't need to send the app to the background or use nohup. Can I ask what it is that red5.sh does? Or could you email me the script via private message if you don't want to post it here.

red5.sh should start red5 server
"Red5 Media Server 1.0 delivers a powerful video streaming and multi-user solution to the ©Adobe ©Flash Player and other exciting client technologies"
but somehow even without screen or nohup in the shell simply /home/ramesh/red5.sh this in the shell does not start red5

thank you for trying to help and not giving up yet

OK try this from their site. Put an entry in /etc/init.d to make it a service that you can start or stop.
http://trac.red5.org/wiki/AppServer/Startup%20Scripts
but when pasting the code from the example STOP at the line that reads

esac

The rest is comments on how to use the service.

once it is listed in /etc/init.d you can set it to start when the system boots by using the command line application
chkconfig
Or use the system GUI and go to services and set it to start.

commented: thank you for not giving up on me, many many thanks +3

how would i start the service in cron?

it worked
Thank you than you thank you

I thank you very much, it worked perfectly
many many thanks

this should be okay right?

31 02,06,10,14,18,22 * * * /mypath/non-red5stop.sh
31 02,06,10,14,18,22 * * * /sbin/service red5 start
31 02,06,10,14,18,22 * * * /mypath/non-red5start.sh

Hello,

Normally when you start a service you should not have to stop and restart it all of the time. You should be able to start the daemon once and it runs and creates additional children as needed. What prompted you to want to continually stop and restart the service?

i dont understand what you meant

You should be able to start the daemon once and it runs and creates additional children as needed

but the reason i needed to do was sometimes red5 stop by itself (actually crashes)if it is not restarted few times a day so that's when i started to look into cron job

thank you

OK then the next thing to do is to find out why it is crashing. One great thing about Unix/Linux is there is a log for everything. Take a look in /var/log and see if there is a log file for the application and also check in /var/log/messages. They are just text files and you may be able to find out what is crashing the app.

Just as side note: in addition to rch1231 suggestions, you can also consider to use a tool like Supervisord, it can help you to keep running processes in background, even if a crash happens Supervisord will restore the process by itself: http://supervisord.org/

bye.

There is one thing about this that I would like to know, the script from their website works perfectly but what about the duplicated process in the background.

Let's say that I am using a crontab which try to load the application every 30min. the issue I am seen here is that when you call the red5 application it does start but as duplicated proccess I would like to check if the proccess is running if so ignore the start otherwise start if process not running. Just in case the proccess shutdown on it's own after while I would like to have a crontab to do that for me.

Any help with this would be appreciated Thanks...

You can check for the PID with getmypid(): http://php.net/manual/en/function.getmypid.php
Or you can use beanstalkd or gearman, these are job servers for scripts: you register a worker, essentially this means that you start a script with a never ending loop that waits to get a work from the job server.

When a client sends a work to the job server this checks if the worker is available and sends the job, if not then it waits until the worker can perform the job. You can register more than one worker (hundreds if you need it) for each task and let the server work. Between beanstalkd and gearman I prefer the second because there is also persistence: job queue can be stored to a MySQL database, so if you restart the job server nothing gets lost.

http://php.net/manual/en/book.gearman.php & http://gearman.org/

https://github.com/kr/beanstalkd

bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.