how to run a script in every 10 minutes [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I need to run a shell script in every 10 minutes.
I am appending the output in a log file every time script runs.
I need to stop the script from running once the size of the log file reached to 10 MB.
how can this be achieved , please help !!
./test.sh >> log_file

maximumsize=10000 #KB
while :
do
./test.sh >> log_file
actualsize=$(du -k log_file | cut -f 1)
if [ $actualsize -gt $maximumsize ];then
echo "logsize exceed.stop."
break
fi
sleep 600
done

You should modify your test script : put your process into a function, add your function into a loop, condition of the loop (size of your log file under 10MB) and at the end of the loop add a sleep 600 to wait for ten minutes

You might add to the start of your test.sh script a test, if the log file > 10MB then do not run (exit), or you could archive (maybe gzip -9 or xz -9) the log, if it's all text output then it should compress a LOT. Or you could pipe the script's output through gzip to compress it right away.
And also use cron/anacron to run the script regularly, like add */10 * * * * user test.sh to /etc/crontab...

Related

What is the advantage of using bash -c over using a here string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
Is there any real benefit to using bash -c 'some command' over using bash <<< 'some command'
They seem to achieve the same effect.
bash -c '...' leaves you the option to provide stdin input to the command,
whereas bash <<<'...' precludes that option, because stdin is already being used to provide the script to execute.
Examples:
# Executes the `ls` command then processes stdin input via `cat`
echo hi | bash -c 'ls -d /; cat -n'
/
1 hi
# The here-string input takes precedence and pipeline input is ignored.
# The `ls` command executes as expected, but `cat` has nothing to read,
# since all stdin input (from the here-string) has already been consumed.
echo hi | bash <<<'ls -d /; cat -n'
/

How to use 'history-c' command in a bash script? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.
I have to trigger this command through my bash script. Script is as follows,
#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi
Output is as follows:
cleared
Though its printing "cleared" as the output, history-c is not deleting the history.
It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.
Thanks & Regards,
Navya
history -c clears the history for the current shell, and does not delete ~/.bash_history.
But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.
Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh
But in either case, the script that you've written does not execute the command. Modify it to something like this:
#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi

How wait for a program to start up and then background it? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I have a program which I'm writing a script for in /etc/init.d.
Problem is, the program does not deamonize itself. It takes like 5 secs to start it and when it has initialized, it prints a string ("Started OK") to stdout.
I'm looking to create script which starts the process, waits a while for the string to appear and then continue the script, indicating failure or success (the string was found).
Obviously this does not work as I want.
daemon $PROGRAM &
Rather
(./proc > some_output) &
poll_output_for "Started OK" 10 secs or die
I think you might be able to use expect to do something like this: see http://expect.sourceforge.net/ or http://en.wikipedia.org/wiki/Expect. This is in repositories for many distributions, e.g. on Ubuntu you can apt-get install expect.
He's a script to simulate your daemon:
#/usr/bin/env /bin/bash
# daemon.sh
sleep 2
echo Started OK
while :; do sleep 1; echo '.' >> daemon.log; done
... and here's an expect script that waits for the output and then exits:
#!/usr/bin/env /usr/bin/expect
spawn -ignore SIGHUP ./daemon.sh
expect "Started OK"
Expect doesn't return until the "Started OK" notification arrives. When it closes the daemon gets a SIGHUP, but we've ignored that so it carries on running. If you inspect the log file (watch cat daemon.log) the daemon should be churning away.
I don't think it should be too hard to get expect to return an appropriate error code - take a look at the manpage for more information.
If I understand you, there's a program that takes 5 or so seconds to start, but runs in the background. You need to wait for this program to start, and this program will still print out "Started OK".
If this program starts, runs in the background, but is still connected to the console, so it can write to the console, you might be able to do something like this:
$prog | tee $prog_out_file
while true
do
sleep 2
grep -q "Started Ok" $prog_out_file && break
done
# Continue here...
The idea is to keep on looping until you see "Started Ok" printed out in the redirected output.

How to debug why my Cron job is running twice? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
When I execute my cron manually everything seems to work. However when it runs by cron it seems to run twice. In my deployment script I have the following two lines to add my crons:
/usr/bin/crontab -l | { /bin/cat; /bin/echo "* 3 * * * /etc/app/execute.py"; } | /usr/bin/crontab -
/usr/bin/crontab -l | { /bin/cat; /bin/echo "* 0,2,4,6,8,10,12,14,16,18,20,22 * * * /etc/app/solr.py"; } | /usr/bin/crontab -
Is there any reasonable reason why my CRON might be running twice on my debian server? I have no idea what might be causing this or how to debug it.
In my Crontab I have this:
* 3 * * * /etc/app/execute.py
* 0,2,4,6,8,10,12,14,16,18,20,22 * * * /etc/app/solr.py
You can debug this by adding something like
; echo $(date) ; echo "Cron line one" >> /root/cronlog
That way you can see which line was executed when.
Also, how do you edit your cronjobs? With "crontab -e" or by directly editing the files? If you edit the files directly (which I don't recommend), then please compare the content of the files with the output of "crontab -l".

Ubuntu - cron not running script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
Ok, so I'm trying to set cron to run a bash script at a certain time. My bash script is essentially this
#!/bin/bash
espeak -g 3 "this is my text"
So from there, I went to the crontabs, and added in
*/1 * * * * /path/to/my/script.sh
to see if it would run, but it didn't do anything. I changed the script to
#!/bin/bash
echo "this is my script"
to see if that would do anything, but no avail. Any help? Thanks.
Try to run the script manually and see if it echos out: bash /path/to/my/script.sh
Does the file have the correct permissions?
Try Outputting errors to a log file: */1 * * * * /path/to/my/script.sh > /path/to/my/error.log 2>&1

Resources