Cron says "errors in crontab file, cannot install" [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm attempting to execute the following series of commands to create backups of MySQL databases.
When I attempt to add the command to my crontab using crontab -e I get the error "errors in crontab file, cannot install" and asks me if I want to retry.
mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "steven#brightbear.net" < output
Is there anything I should be changing in this file? Or should I look into creating a script file and calling that from cron.
Thanks in advance for any assistance you can provide.

If you gave that script to crontab -e of course it will disagree. A line in a crontab file should start with 5 fields indicating when you want the script to run, as can be read in crontab's manpage.
On the other hand, most Linux distros nowadays have preset facilities for things that should be executed hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's a whole lot easier to just put your script in a file in the appropriate directory and it will get executed in the selected time raster. An added advantage is that in these files you won't be forced to cram everything into one line.

Yes; as a matter of style, if nothing else, I encourage to put the SQL commands into a shell script, and then run the shell script from cron.  (And, as Anew points out, the command sequence is easier to maintain/debug if it’s broken out into multiple lines, with comments.)  But –– is that all of what you’re feeding into crontab?  Look at man crontab and add the fields that specify when you want the command to run.

From the crontab(5) man page, it looks like percent signs (%) have a special meaning, so that is probably where you're running into trouble.
Yes, you should put your commands into a separate shell script and just call that from the crontab line. This will also make it much easier to read the crontab file, and you can format your script nicely so that it's easier to maintain. And you can test it separately from the crontab that way.

Related

Crontab on linux [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand, I have looked at several forums to help me, etc... But it still doesn't work!
I would like to run a script thanks to Cron!
To try, I'm currently trying to execute a script that sends "test" in commands (with echo test). No problem, this script works perfectly by hand if I call the file.
But I tried to run my file with Crontab -e every minute and I've been waiting for several minutes already, but no results.
And I can't really understand why!
Already, I was told to put #! /bin/bash at the beginning of my code in my script, but when I put it in I have an error and the code doesn't execute by hand. Whereas if I don't put anything in, the code runs smoothly.
So I don't know if that's where the mistake came from, maybe.....
The final goal would be to make a script that runs every day, say to clear the cache with sync; echo 3 > /proc/sys/vm/drop_caches.
What should look like in the Crontab : 00 20 * * * PATH if I'm not mistaken.
Do you have a solution to help me?
EDIT: -bash: /root/Discord/script/cache.sh: /bin/bash^M: bad interpreter: No such file or directory it's the error I got when I runned /root/Discord/script/cache.sh to execut my script. And that command works when I don't have #! /bin/bash. But that works when I used sh cache.sh in the directory, even with #! /bin/bash !
Crontabs don't print the output on your opened terminal. You need to either create a file and then append the output there to view or test if it works. You can refer this answer https://stackoverflow.com/a/28856563/7181668
But if you want to run a shell script file through cron then you need to make sure you have given executable permission to that file and then you can use the below command in crontab -e
* * * * * /bin/sh /home/myUser/scripts/test.sh

What does "read -p" do in a linux shell 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
I have a script that I have copied and edited. There are a couple of lines in there that I need explaining if possible please.
These are the lines:
read -p "please enter the username you wish to create: " username
if id -u $username >/dev/null 2>&1; then
What does read -p do? What does id -u do? What does >/dev/null 2&1; do?
Then further on in the script, it has this line that says this:
sudo useradd -g $group -s $bash -d $homedir -m $username -p $password
Again please could someone explain all the minus signs in this line please? (-g, -s, -d, -m, -p)
First off, the structure <command> -<option> means that you want to execute <command> using the option corresponding to <option>. A - after a command means that the following letter is an option. Most commands have several options you can use. Options are usually defined using either a single letter or a couple of words separated by -.
Side Note: For options that are a couple of words rather than a single letter, often it will use two minus signs -- instead of one, signifying that it is a "long named" option.
So, using the read -p example, this means you want to execute read using the p option, which stands for prompt.
Now, sometimes an option will require an argument. In your examples, the options to useradd have arguments. Arguments are usually defined like <command> -<option> [argument]. So, in the useradd example, $group is an argument for the option g.
Now for the commands themselves:
read is a bash built-in (not a POSIX shell command) that reads from standard input.
The -p option makes it read as a prompt, meaning it doesn't add a trailing newline before trying to read input.
if checks the return status of the test command (in this case id -u $username >/dev/null 2>&1)
If the return status is 0, the then part is executed
id prints user groups and ids
The -u option "prints only the effective user ID".
>/dev/null 2>&1 redirects standard input and standard error to /dev/null, meaning they do not get printed to the terminal.
useradd creates a new user
-g sets the initial group for the user
-s sets the name of the user's login shell
-d sets the name of the user's login directory
-m says to create the user's home directory if it does not exist.
-p defines the user's encrypted password.
For future reference, you can look up commands in the linux manual pages by doing man <command> on the command line. These manual pages tell you what a command does, as well as explaining all of its options.
Bash built-ins like read are all on a single man page that is not the easiest thing to use. For those I find googling them easier. Usually http://ss64.com/ will come up in the results, which contains the info from the bash built-ins man page, but separated into different pages by command. I find this much easier to use.

How do I make a script or program to run certain commands? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get into writing scripts that execute in the terminal. I wanted to know what the best way to do this was. I want to start by making a simple script that will run four or five commands that will update a certain program on my computer and have that run every day at a certain time. I have a programming background, but I am unfamiliar with this kind of scripting. I would appreciate any advice or input such as what language to use.
First of all, you need to open a Terminal (such as Terminal, Terminator, etc) and then you run this:
touch myScript.sh
chmod 755 myScript.sh
The first command creates an empty file and then you give 755 permissions to it. It means that it will be readable and executable by any user in your machine. If you need more details about those permissions, you can refer to the documentation here. But, believe me, those permissions will work for the moment.
Now you can insert instructions into the file using several methods: You can open it with a text editor such as vi, etc; Also, you can echo those commands this way:
echo "ls /tmp" >> myScript.sh
echo "echo 'hello'" >> myScript.sh
echo "pwd" >> myScript.sh
If you open that file, you can find that it is simply a list of commands one at each line. Then, when you run the script, each command will be executed in order from top to bottom.
You can run the script using the following sintax:
./myScript.sh
Voilá!
crontab -e
Then add following line beside the above command:
30 10 * * * script.sh
It will run everyday at 10:30 AM

saving cron job in ubuntu [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am new to ubuntu and cron job. I have entered the following into my command line:
crontab -e
and I get the following output: "no crontab for teddy - using an empty one 888"
Then I enter when I want it to execute (I believe this is right?... I want it to run once a day, everyday at 8pm):
00 18 * * * /*****/*****/****/test.php
Here is my problem, I dont know how to exit back to the command line. Everything I type gives me weird looking letters and enter (return) doesn't do anything. I have read that this will do the job
ESC : w q
but its not working for me. I tried typing that in, I tried pressing them at the same time, I tried pressing one at a time. Nothing, still stuck. When I press ESC, it comes out as ^[.
This is probably very easy question and I apologize if it is stupid but I have been stuck for sometime. Any help would much appreciated.
Thank you
P.S. I read somewhere that if this is your first job that you need to to do an end line at the end of the cronjob... is this a simple enter key press or actually typing \n?
Teddy13, let's get some clarifications here.
Ubuntu is the distribution of Linux you are using. None of the commands you're typing are exclusive to Ubuntu.
You're asking questions about two separate issues. One is "How do I write a crontab". The other is "How do I use vi, the default editor of the crontab command".
First, man crontab to review the format for entries in the file. Note that cron runs things that are executable from the shell. You can only run your "test.php" script if it is structured like a shell script, with the first line containing "shell magic" (i.e. something like #!/usr/local/bin/php).
Second, while vi is a powerful and well-loved text editor, it's not the easiest to use. I fully support any efforts you may make to learn how to use it, but until you're comfortable with it, you might want to consider switching to "pico" or "ee" or "joe", which are all much easier to learn, though they can do much less. You can install joe, for example, with the command: apt-get install joe run as root. Then to use joe to edit your crontab, add export VISUAL=/usr/bin/joe to the .bashrc file in your home directory.
There's a lot of background information you may want to get. Read lots. It's worth it.
UPDATE (per comment):
Here's the basic stuff you need to edit your crontab.
crontab -e ... as you now know, this edits your crontab file using $EDITOR or $VISUAL, which defaults to vi.
Inside vi, you are always in one of three modes. MOVEMENT mode lets you move around the file using arrows or H, J, K and L. You can delete lines with "dd" or characters with "x". EDIT mode lets you add or change text. From Movement mode, use "i" or "a" or "o" to enter Edit mode in different ways. Read docs for details. Third, COMMAND mode can be reached by hitting ":" from Movement mode. From here, you can issue a variety of commands to save, search, bulk edit, etc.
From Movement mode, you can save your file and exit using "ZZ". From edit mode, you can do this with the command "wq" (hence the ":wq" mentioned elsewhere).
Alternately, you can set a new crontab by piping information into the crontab command. Note that this will erase any existing crontab you may have. Run this in your shell updating the URL of your script as required:
echo "0 20 * * * wget http://example.com/path/to/file.php" | crontab -
crontab -l will show you what your current crontab contains.
Hope this helps.
UPDATE #2 (per comments below):
$ tmpfile=/tmp/foo.$$
$ crontab -l > $tmpfile
$ echo "30 6 * * * Mail -s wakeup pager#example.net <<< 'Time to wake up.'" >> $tmpfile
$ crontab - < $tmpfile && rm $tmpfile
You can try Ctrl+X. This will ask you for changes to save or not before exit. In that just give Shift+Y so you will get out of there. Try it.
You need to press ZZ to exit (upper case 'z' twice) . Here is an article on setting up cron tabs - http://www.sophos.com/support/knowledgebase/article/12176.html

How do I set up cron to run a file just once at a specific time? [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 2 years ago.
Improve this question
How do I set up cron to run a file just once at a specific time? One of the alternatives is at but it is not accessible to all users on standard hosting plans. Therefore I was wondering whether there is way to do it using cron?
You really want to use at. It is exactly made for this purpose.
echo /usr/bin/the_command options | at now + 1 day
However if you don't have at, or your hosting company doesn't provide access to it, you can have a cron job include code that makes sure it only runs once.
Set up a cron entry with a very specific time:
0 0 2 12 * /home/adm/bin/the_command options
Next /home/adm/bin/the_command needs to either make sure it only runs once.
#! /bin/bash
COMMAND=/home/adm/bin/the_command
DONEYET="${COMMAND}.alreadyrun"
export PATH=/usr/bin:$PATH
if [[ -f $DONEYET ]]; then
exit 1
fi
touch "$DONEYET"
# Put the command you want to run exactly once here:
echo 'You will only get this once!' | mail -s 'Greetings!' me#example.com
Try this out to execute a command on 30th March 2011 at midnight:
0 0 30 3 ? 2011 /command
WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.
You really want to use at. It is exactly made for this purpose.
echo /usr/bin/the_command options | at now + 1 day
However if you don't have at, or your hosting company doesn't provide access to it, you could make a self-deleting cron entry.
Sadly, this will remove all your cron entries. However, if you only have one, this is fine.
0 0 2 12 * crontab -r ; /home/adm/bin/the_command options
The command crontab -r removes your crontab entry. Luckily the rest of the command line will still execute.
WARNING: This is dangerous! It removes ALL cron entries. If you have many, this will remove them all, not just the one that has the "crontab -r" line!
You could put a crontab file in /etc/cron.d which would run a script that would run your command and then delete the crontab file in /etc/cron.d. Of course, that means your script would need to run as root.
Your comment suggests you're trying to call this from a programming language. If that's the case, can your program fork a child process that calls sleep then does the work?
What about having your program calculate the number of seconds until the desired runtime, and have it call shell_exec("sleep ${secondsToWait) ; myCommandToRun");
at is the correct way.
If you don't have the at command in the machine and you also don't have install privilegies on it, you can put something like this on cron (maybe with the crontab command):
* * * 5 * /path/to/comand_to_execute; /usr/bin/crontab -l | /usr/bin/grep -iv command_to_execute | /usr/bin/crontab -
it will execute your command one time and remove it from cron after that.
For those who is not able to access/install at in environment, can use custom script:
#!/bin/bash
if [ $# -lt 2 ]; then
echo ""
echo "Syntax Error!"
echo "Usage: $0 <shell script> <datetime>"
echo "<datetime> format: %Y%m%d%H%M"
echo "Example: $0 /home/user/scripts/server_backup.sh 202008142350"
echo ""
exit 1
fi
while true; do
t=$(date +%Y%m%d%H%M);
if [ $t -eq $2 ]; then
/bin/bash $1
echo DONE $(date);
break;
fi;
sleep 1;
done
Let's name the script as run1time.sh
Example could be something like:
nohup bash run1time.sh /path/to/your/script.sh 202008150300 &

Resources