Insert a cronjob via bash - linux

I'm trying to create a very simple bash script where I need to list,insert and remove my cronjobs.
I'm doing the listing using crontab -l and I remove the all using crontab -r. But when i wanna insert one, my code does not work (it does not actually add the cronjob to /etc/crontab),even it does not throw any error. My code is the following:
echo "Time to be Executed"
echo -m "Enter minute:"
read m
echo -h "Enter hour:"
read h
echo -dom "Enter day of month:"
read dom
echo -mon "Enter month:"
read mon
echo -dow "Enter day of week (number or first three characters ex 1 or Mon):"
read dow
echo -j "Enter job to be executed:"
read j
echo "$m $h $dom $mon $dow root $j" >> /etc/crontab;
Do you see anything that i've done wrong here? Any help would be highly appreciated,thanks!!

I've tested your script and it works. Make sure you have the right permissions (sudo ./myscript.sh).
Perhaps replace the last line with:
echo "$m $h $dom $mon $dow root $j" | sudo tee -a /etc/crontab

Related

executing bash script via CGI

everyone!
I have a trouble with executing .sh script from browser.
echo command works well. But when I store the value in variable and pass it to another script as an argument it does not work.
Appreciate any help.
Example:
Here I pass $XX to another script
if [ -z "$QUERY_STRING" ]; then
exit 0
else
XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
if [ -z "$XX" ]; then
echo "X is empty"
else
echo "Processing..."
/sas/oljas_scripts/find_usage_of_tables.sh $XX
echo "Done"
fi
YY=`echo "$QUERY_STRING" | sed -n 's/^.*val_y=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
ZZ=`echo "$QUERY_STRING" | sed -n 's/^.*val_z=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
echo "val_x: " $XX
echo '<br>'
echo "val_y: " $YY
echo '<br>'
echo "val_z: " $ZZ
fi
The file you are trying to execute, /sas/oljas_scripts/find_usage_of_tables.sh, is not executable for the web server. Currently, it is:
-rwxrw-r-- 1 sas sas 1540 Nov 12 12:58 find_usage_of_tables.sh
Which means sas user can do everything, sas group can read and write but not execute, and everyone else can just read. Notably, the web server needs to be able to both read and execute the file, and it is failing to do so.
Either set the file to be readable and executable to all (not recommended):
chmod 755 /sas/oljas_scripts/find_usage_of_tables.sh
or set the group of the file to be a group the web server is in, and set the group permissions of the file accordingly:
chmod 754 /sas/oljas_scripts/find_usage_of_tables.sh
chgrp www-data /sas/oljas_scripts/find_usage_of_tables.sh
You may need sudo for the latter. Also, the web server's group name might not be www-data - inspect your web server configuration, or read it off of process list like this answer suggests.

Script in crontab to be executed only if is equal or exceeds a value

I currently have a script in crontab for rsync (and some other small stuff). Right now the script is executed every 5 minutes. I modified the script to look for a specific line from the rsync part (example from my machine, not the actual code):
#!/bin/bash
Number=`/usr/bin/rsync -n --stats -avz -e ssh 1/ root#127.0.0.1 | grep "Number of regular files transferred" | cut -d':' -f 2 | tr -d 040\054\012`
echo $Number
Let's say the number is 10. If the number is 10 or below I want the script executed through the crontab. But if the number is bigger I want to be executed ONLY manually.
Any ides?
Maybe you can use an argument to execute it manually, for example:
if [[ $Number -le 10 || $1 == true ]];then
echo "executing script..."
fi
This will execute if $Number is less or equal to 10 or if you execute it with true as the first positional argument, so if $Number is greater than 10 it won't execute in your crontab and you can execute your script manually with ./your_script true.

Script to check the alteration of crontab

Hi I Need a script to monitor the cron entries and if there is any change in the crontab entry it needs to alert me.
For this I have used the method by taking a copy of current crontab then it needs to compare the crontabs every day.If there is any alteration found it needs to alert me.Is this possible?
Currently I'm using crontab -l > $(date +%Y%m%d).crontab command to take copy every day and I think diff command can do the comparison
can any one please help?
You could try something like below script
CRDI=/var/spool/cron/crontabs
CHECKF=/tmp/last.crontab.check
ALERT=iam#userhost
if [ -f $CHECKF ]
then
find $CRDI -type f -newer $CHECKF | while read tabfile
do
echo "Crontab file for user $(basename $tabfile) has changed" | mail -s "Crontab changed" $ALERT
done
fi
touch $CHECKF
I finally got the answer Thanks for your efforts
Hi, I finally got the answer Thanks for your efforts
#!/bin/sh
export smtp=smtprelay.intra.xxxx.com:25
ALERT=redmine#xxxx.com
crontab -l > /home/ssx00001/y.txt
cat y.txt
diff /home/ssx00001/x.txt /home/ssx00001/y.txt > /home/ssx00001/z.txt
ab=`cat z.txt | wc -l`
echo $ab
if [[ $ab != 0 ]]; then
echo "Crontab for xxxx has changed" | mail -s "Crontab modified" $ALERT
fi

How to output the start and stop datetime of shell script (but no other log)?

I am still very new to shell scripting (bash)...but I have written my first one and it is running as expected.
What I am currently doing is writing to the log with sh name-of-script.sh >> /cron.log 2>&1. However this writes everything out. It was great for debugging but now I don't need that.
I now only want to see the start date and time along with the end date and time
I would still like to write to cron.log but just the dates as mentioned above But I can't seem to figure out how to do that. Can someone point me in the right direction to do this...either from within the script or similar to what I've done above?
A simple approach would be to add something like:
echo `date`: Myscript starts
to the top of your script and
echo `date`: Myscript ends
to the bottom and
echo `date`: Myscript exited because ...
wherever it exits with an error.
The backticks around date (not normal quotes) cause the output of the date command to be interpolated into the echo statement.
You could wrap this in functions and so forth to make it neater, or use date -u to print in UTC, but this should get you going.
You ask in the comments how you would avoid the rest of the output appearing.
One option would be to redirect the output and error of everything else in the script to /dev/null, by adding '>/dev/null 2>&1' to every line that output something, or otherwise silence them. EG
if fgrep myuser /etc/password ; then
dosomething
fi
could be written:
if fgrep myuser /etc/password >/dev/null 2>&1 ; then
dosomething
fi
though
if fgrep -q myuser /etc/password ; then
dosomething
fi
is more efficient in this case.
Another option would be to put the date wrapper in the crontab entry. Something like:
0 * * * * sh -c 'echo `date`: myscript starting ; /path/to/myscript >/dev/null 2>&1; echo `date`: myscript finished'
Lastly, you could use a subshell. Put the body of your script into a function, and then call that in a subshell with output redirected.
#!/bin/bash
do_it ()
{
... your script here ...
}
echo `date`: myscript starting
( do_it ) >/dev/null 2>&1
echo `date`: myscript finished
Try the following:
TMP=$(date); name-of-scipt.sh; echo "$TMP-$(date)"
or with formatted date
TMP=$(date +%Y%m%d.%H%M%S); name-of-scipt.sh; echo "$TMP-$(date +%Y%m%d.%H%M%S)"

bashscript is not getting invoked in current shell through crontab

everyting working as root user in linuxmint. my bashscript works perfectly as a single
script and generating logfile as well but it is not getting invoked in my current shell through crontab.
path of bashscript: /root/Documents/mybashscript.sh
crontab line:
0.9 * * * * root /root/Documents/mybashscript.sh > /root/Documents/crontab.log
mybashscript.sh inside commands are as below:
#!/bin/bash
source /root/.profile
echo -n "Please enter your name: "
read name
TIME=‘date +%H‘
case $TIME in
0[6-9] | 1[01] ) echo -n "Good morning";;
12 ) echo -n "Good Noon";;
1[2-6] ) echo -n "Good Afternoon";;
1[7-9] ) echo -n "Good Evening";;
*) echo -n "Good Night";;
esac
echo " $name, Nice to meet you!"
Can anybody tell me how to trouble shoot.
First things first, I don't believe 0.9 is a valid minute indicator. I think you probably want 0-9 (depending on when you want it to run of course).
Secondly, you appear to have an extraneous root in your cron entry (the first one following the final *).
Thirdly, cron jobs don't really work that well for interactive input like:
read name
so I'm not sure what you're trying to achieve there.

Resources