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
Related
I created this test bash script
#! /bin/bash
echo "hello"
mkdir "/karan/washere"
cron job i created, i want to run this cron job to run every min and want the log
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I am signed in the droplet as root user
I also have given the permission for the script, using
sudo chmod u+x test.sh
I tried to log the syslog using
sudo grep CRON /var/log/syslog
but didn't show there also,
let me know if you need any more info or other context,
cron uses sh to interpret the commands, not bash. sh has less features and the &> is not valid in sh.
A better way for your line in the crontab would be:
1 * * * * /user/local/bin/bash /root/test.sh >> /root/crontest.log 2>> /root/crontest.err
This will append to the logging instead of overwriting, and it will separate log from errors.
If you want, however, you can force cron to use bash instead of sh. Your crontab should then be:
SHELL=/bin/bash
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
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 -
i wrote a sh file which kills a tomcat process, suppose my shell scrip is in /usr/temp, script looks likethat:
#!/bin/bash
ps -Af | grep "tomcat_something" | grep -v grep | awk '{print$2}' | xargs sudo kill -9
then i created a cron via crontab -e My cron looks likethat (dont pay attention to the time):
10 10 * * * /usr/temp/myshellscript.sh
If i run the sh without cron it works fine, but via cron is not working at all.
I even tryied to run the same shell script with a touch file command and in this case cron works fine, so my doubt is that the problem is the kill command. Can someone help ?
Problem:
sudo in cron job does not work as it expects a password to be entered.
Possible Solutions
Not Recommended as require to give password in plaintext.
Use expect (Quick google or SO search will help you) in your script
and provide sudo's password there.
Run your script as sudo instead of running sudo inside script
In your own crontab, write your command like so:
10 10 * * * echo "your password" | sudo -S /usr/temp/myshellscript.sh
Again, it is not recommended
Best Possible Way: Use roots crontab (only if you have access)
Run the following command:
sudo crontab -e
This opens up root's crontab. sudo is not necessary to run your command in this context since it'll be invoked as root anyway.
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
Hi guys am trying to automate the backup of snapshots for my ec2 volumes on Amazon. I am following the ec2-automate-backup script by Collin Johnson
If run the command on command line it is creating the snapshot (working):
ubuntu#linuxserver:/usr/local/ec2/scripts$ sudo ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
For testing purposes if i create a crontab its not working
0 10 * * * ubuntu /usr/local/ec2/scripts/ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c /usr/local/ec2/scripts/cron-primer.sh -r "eu-west-1"
Where is my problem here am running the script on ubuntu 14.04 - Amazon?
In crontab file, to execute a shell script you can use one of the following approach:
1. Call the shell script direcly, i.e.
0 10 * * * /path/to/script.sh
where the script.sh should be made executable.
2. Execute the script by sh utility, i.e.
0 10 * * * sh /path/to/script.sh
here the script.sh need not be made executable.
Now, if in your case, you need to go to a specific path and then execute script, then :
Either provide the full path of the script in crontab file directly, or
Enclose the execution commands in other shell file, and execute the enclosing file from cron.
There are two possibilites:
You need root access to run the script. You can solve this by modifying root's crontab:
sudo crontab -e
See How to run a cron job using the sudo command
You need to be in the same directory as the script to execute it
0 10 * * * ubuntu cd /usr/local/ec2/scripts && ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
See What is the 'working directory' when cron executes a job