I am trying to run a python script every 5 minutes.
I type the following in terminal after doing crontab -e
*/5****/Users/nishantarora/Documents/cronjob.py (no spaces at all in between)
And this is what I get :
crontab: installing new crontab
"/tmp/crontab.TVJ8UDlEMk":1: bad hour
crontab: errors in crontab file, can't install
Please help me out.
As you mentioned, you did not place any space anywhere. It becomes then also very difficult do understand your line.
The format of a cron command is very much the V7 standard, with a number of upward-compatible extensions. Each line has five time and date fields, followed by a user name if this is the system crontab file,
followed by a command.
source: man 5 crontab
Since they talk about fields, it is evident that these must be white-space delimited.
The error you obtain, i.e. bad hour crontab, makes sense since you only have a single field which represents the minute field, that field, is a bit ill-formatted. I'm surprised it did not complain about that.
Solution to your problem, add a couple of spaces.
*/5 * * * * /Users/nishantarora/Documents/cronjob.py
Related
My expectation is that the below instructions will make my script execute every 5 mins from 0930 to 1730 on weekdays.
30/5 9 * * 1,5 /home/main/.bin/somescript.sh
*/5 10-16 * * 1,5 /home/main/.bin/somescript.sh
0-30/5 17 * * 1,5 /home/main/.bin/somescript.sh
I installed cronie and tried to provide these instructions using crontab -e. I get the following output:
no crontab for main - using an empty one
crontab: installing new crontab
"/tmp/crontab.6hY2xA":1: bad minute
Invalid crontab file, can't install.
I read a tip somewhere that the first and last line can not have cronjob instructions, I've attempted that, but get the same error.
What is my mistake?
The crontab(5) man page (man 5 crontab) says:
Step values can be used in conjunction with ranges. Following a
range with "/" specifies skips of the number's value through
the range. For example, "0-23/2" can be used in the hours field to
specify command execution every other hour (the alternative in the V7 standard
is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after
an asterisk, so if you want to say "every two hours", just use
"*/2".
So a step value like "/5" can be used with a range or an asterisk, but not with a single number.
I had to edit the minute of the first line to 30-59/5
I have the perl file named as perl.pl. I want this file to run and output the contents in new file using crontabs?
I dont know the steps how to perform it?Can anyone explain me with screenshots
You will need to open crontab by using:
crontab -e
This will open crontab in a text editor (usually this is vi or vim). Once inside the editor you will want to add:
* * * * * /usr/bin/perl <PATH_TO_DIR>/perl.pl
This will run perl.pl once every minute. The five asterisks mean that the script will run every minute, every hour, every day of month, every month and every day of the week.
For more information on how crontab works read over some of the examples on this page.
I'd suggest a search before asking a question like this: https://stackoverflow.com/a/5398044/225905. TL;DR using all * for hour, month, day, and year will give you crontab once a minute. For example:
* * * * * /path/to/php /var/www/html/a.php
Once a minute is quite frequent for cron, and cron won't repeat any faster than that. It may be better to have your perl script run in the background and repeat its calculation once a minute.
This is my first question on StackOverflow. I am pretty sure it would have been answered already (it is a pretty dumb question, I think, as I just started to learn Linux scripting), but I did'nt succeed to find an answer yet.
Sorry for that.
Here is my problem: I try to use in a shell a number given in a property file, I have an error because the number is not taken "as it is".
I have a prop.properties file :
sleepTimeBeforeLoop=10
and a test.sh shell :
#!/bin/sh
. prop.properties
echo "time="$sleepTimeBeforeLoop
sleep $sleepTimeBeforeLoop
When I launch test.sh I have the following Output:
time=10
sleep: invalid time interval `10\r'
Try `sleep --help' for more information.
What I understand is that my properties files was correctly sourced, but that the property was taken as a string, with some special character to indicate the end of the line, or whatever.
How can I do to take only the "10" value?
Thank you in advance for your answer.
This is line endings issue / make sure your script files are terminated by \n (the unix way)
eg. write it in UNIX text editor or use a windows one capable of saving in "unix style"
Is is possible to have the history of a specific user in one more file other than the default file mentioned as HISTFILE?
I would like to have as backup file if main file was removed and let it be like a backup for that one.
Regards,
Sriharsha Kalluru.
You can create a hardlink to the file
cp --link --verbose /home/$USER/.bash_history /somewhere/else/users_history
When the original file in his home is removed the file is still there and preserves the content from being lost.
Many times I've found myself using Ctrl-R in Bash to get a old command four times the terminal width, just to find out that too many days have passed and it's no longer in the .bash_history file. Here are two lines that will keep track of every command line you type at the bash prompt and use no external processes at all, just plain bash.
My first approach to this problem was increasing the maximum number of lines in the history to a very large quantity. But no matter how large it was, there was always a moment when I needed a long command I typed many months ago and it had already left the history. The current solution came to my mind when I learned about the PROMPT_COMMAND variable, a command that bash executes before showing each prompt. Here are the two lines:
export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
"$(history 1)" >> ~/.bash_eternal_history'
One goal I set to myself was to achieve it without using any external process, so bash wouldn't have to fork a new process after every ENTER pressed at its prompt. The first line sets the format of history lines to include the date as a Unix timestamp, so you can now when you typed every command. The second line, which is the core of the solution, first ensures that, if a previous PROMPT_COMMAND was set, it gets executed before our stuff and then appends a line of the format:
PID USER INDEX TIMESTAMP COMMAND
to a file called .bash_eternal_history in the current user home.
Adding the username, which at first seemed unnecesary, became useful later to distiguish between "sudo -s" sessions and normal sessions which retain the same value for "~/", and so append lines to the same .bash_eternal_history file.
I hope some of you find these two lines as useful as I do. :-)
Would hard links solve your problem?
Also you can read the man page here.
i would write a cronjob that copies the original histfiel to a backuplocation every minute or so. the you don't have to worry about defining a second histfile.
otherwise you could write every command the user enters to a alternate file
for this approach take a look here:
bash, run some command before or after every command entered from console
I know that default cron's behavior is to send normal and error output to cron's owner local email box.
Is there other ways to get theses results (for example to send it by email to a bunch of people, to store them somewhere, and so on) ?
To email the output to a different email address just add the line
MAILTO="user#example.com"
To the crontab before the command
You could chuck file redirection onto either the command shown or the actual command in the crontab for both stdout and stderr - like command > /tmp/log.txt 2>&1 .
If you want several users to receive this log, you could insert a MAILTO=nameofmailinglist at the top of you cron file.
The cron line is just like any other unix command line so you can redirect output to another program. Ie.
* * * * * /path/my/command > /my/email/script 2&>1
This may be an unnecessary addition, but to qualify the redirection commands:
> redirects standard output
2 is a Bourne shell specific term that means standard error
1 is a Bourne shell specific term that means standard output
2>&1 means redirect the standard error to standard output
Also see the following useful article Standard Input and Output Redirection
As far as I see it you've got three options:
Redirect the output: either to a file, or to a program that will email the results as you want them
Use MAILTO in cron, and redirect the email to any other single address for all your cron jobs.
Do the redirection in your mail server or client, after cron has sent it.