I have a script that is executed by the user "www-data" that requires adding a couple of cronjob that run under the user "test".
#!/bin/bash
echo "* * * * * test /usr/local/bin/test.sh" > /etc/cron.d/myjob
Fails because obviously www-data has no permissions to write to that folder and I doubt changing the folder permissions is a good idea.
#!/bin/bash
(crontab -u test -l ; echo "* * * * * /usr/local/bin/test.sh") | crontab -u test -
Gives must be privileged to use -u despite adding www-data ALL=(root) NOPASSWD: /usr/bin/crontab to visudo.
It must be noted its not possible to ssh into the server to manually add the job. I'm trying to do this through a web based interface that allows user to select some values, and then run a script on the box to set it up.
How can I have "www-data" create a cronjob for the user "test"?
In order for the sudo privileges you added to have any effect at all, you have to actually use sudo.
#!/bin/bash
( crontab -u test -l;
echo "* * * * * /usr/local/bin/test.sh") |
sudo crontab -u test -
# ^^
Basicly you cannot do this without superuser access because this is a security violation.
Since we're most probably talking about shared hosting, probably there is no way to configure sudo rules or create files inside /etc/cron.d/, but there may be some UI to configure cronjobs.
#tripleee
Thanks for putting me on the right path. Putting sudo infront of both commands fixed the problem.
#!/bin/bash
( sudo crontab -u test -l;
echo "* * * * * /usr/local/bin/test.sh") |
sudo crontab -u test -
Related
I've made a bash script that execute a docker command to dump a MySQL database:
dump_db.sh
#!/bin/bash
time=$( date +%Y%m%d%H%M )
currdir=$( pwd )
cat $currdir/container_list | while read container; do
echo "" | docker exec -i $container mysqldump -u <user> -p<password> <dbname> > $currdir/$container-$time.sql
done
If i try to run the script manually, all works fine, but if i use cron, the script does not execute.
My crontab is:
PATH=<same environment>
26 17 * * * /bin/sh /path/to/script/dump_db.sh
as you can see, i've tried also to export PATH (and checked it via env command in the crontab), but nothing.
I've also tried with these lines
26 17 * * * /bin/bash /path/to/script/dump_db.sh
26 17 * * * /path/to/script/dump_db.sh
Furthermore, also a simple bash script, like this:
#!/bin/bash
touch test_touch.txt
does not work, while the simple touch command via cron it is ok.
Where am i wrong?
Kindly check if the script has execution permission for other user.
When you're running manually you're running with the user you're logged in.
But when its running from Cron its another user. Sot that user has to have the permission to execute that script.
You may check this post for more help
I am using laravel to run my cron.The problem is it that all the commands are running twice.
When i add ps -A in my terminal it shows two enteries.
964 ? 00:04:56 cron
25010 ? 00:00:00 cron
I have this entry in laravel :
* * * * * php /var/www/war-circle-web/artisan schedule:run >> /dev/null 2>&1
What could be the problem?
First you need to check for what users cron runs
sudo ls /var/spool/cron/crontabs
you'll see few names or one - it depends
user_name_1 user_name_2
Then you have options for:
1) update your own cron
crontab -e
2) remove you cron
crontab -r -u user_name
3) remove cron for another user
sudo crontab -r -u user_name
4) update cron for specific user_name
sudo crontab -e -u user_name
I am attempting to run a script through crontab that is required to run as an oracle user. I have tried creating a crontab for that user by:
su -u oracle crontab -e which has allowed me to create one. I edited the file to run a perl script:
0 5 * * * /usr/bin/perl /path/master.pl > /tmp/debug.log
However when the time passes nothing is run.
Is this the proper way to create a crontab for non-root user? Also the master.pl file call multiple scripts that also need to be done as a oracle user if that makes a difference.
Use crontab's -u option. The man page says:
-u Appends the name of the user whose crontab is to be modified.
If this option is not used, crontab examines "your" crontab,
i.e., the crontab of the person executing the command. Note
that su(8) may confuse crontab, thus, when executing commands
under su(8) you should always use the -u option. If no
crontab exists for a particular user, it is created for him
the first time the crontab -u command is used under his
username.
So the correct command is:
sudo crontab -e -u oracle
I have Ubuntu and am running under the user "alex". I've got the following bash script running as root with crontab however it sends me an email and it looks like it hasn't run correctly as the result is not present:
/dev/sda - Reallocated_Sector_Ct is
However if I run the crontab job manually from webmin, it works without issues. But when it's scheduled to run, that's when it fails. Maybe it's not really running as root?
Here's my code:
#!/bin/bash
SMARTCHECK=`smartctl -a /dev/sda | grep "Reallocated_Sector_Ct" | awk 'NR==1 {print $10}'`
echo "/dev/sda - Reallocated_Sector_Ct is $SMARTCHECK"
if [ "$SMARTCHECK" != "0" ]; then
mail -s "Failing: /dev/sda" alex <<< "/dev/sda - Reallocated_Sector_Ct is $SMARTCHECK"
fi
Thanks!
There are two possibilities to run scripts using cron:
You use system crontab /etc/crontab. In this case they run as root.
You use users' crontabs, which are accessible using crontab -e. In this case they run with privileges of the user, that has added the command to his crontab.
more possible issues in cronjob scripting.
change all commands in script with full path, especially the command smartctl
check your system has /bin/bash, not /usr/bin/bash, or set the cronjob as:
0 * * * * ~/.profile;/usr/bin/bash YOUR_SCRIPT
check the mail in alex account, if there are any error messages can be found.
I have a script that creates some temporary files that need to remain in place for quite some time. I want the user who executes the script to be able to create their own custom crontab that removes these files at a later time.
To test, I've just simply tried to setup a simple crontab using the command-line exclusively, but I'm not sure if this is even possible.
From the command-line I type the following:
$ crontab 1 * * * * $(mkdir -p ~/Desktop/CronSuccess)
I get the error: crontab: 1: No such file or directory
Is there anyway to have a script create a fully functional crontab on the fly?
Read the manpage for crontab
You can't do what you're trying to do.
You can however have crontab read the entries from a file.
e.g.
echo 'crontab 1 * * * * mkdir -p ~/Desktop/CronSuccess' >mycrontab
crontab mycrontab
Be aware that this will not append to the users crontab, it will replace the existing crontab of the user
with what's in the mycrontab file.
Surrounding the mkdir command with $() would be wrong.
You might also use at if it suits your needs:
e.g.
echo 'mkdir -p ~/Desktop/CronSuccess' | at now + 10 hours