I'm new here, I hope they can help me with a problem I have. Thanks ...
I need to run a file with expect through the crontab on the Ubuntu Server.
The following command works:
# expect /tmp/teste.sh
However the crontab (# crontab -e) does not work:
1 * * * * root expect /tmp/teste.sh
The content is /tmp/teste.sh:
#!/bin/sh
#!/usr/bin/expect -f
spawn mysql -u root -p
expect "password"
send "sssss\n"
expect "mysql"
send "CREATE DATABASE bbbbb;\n"
expect "mysql"
send "exit;\n"
expect eof
exit
Could you help me?
This command works
expect /tmp/teste.sh
but cron runs it in a specific environment which needs to have a path to your expect application.
For me this usually solves the problem:
1 * * * * root /path/to/the/binary/expect /tmp/teste.sh
Probably you'll need to add the path in the script as well.
Related
I'm trying to run a cronjob to start and stop a server under a non-sudo user. I've tried asking others and doing what I saw from looking on google before asking here, but I'm still stuck.
Here's what's in my crontab for the server user:
* * * * * /home/server/startup/stop.sh
* * * * * /home/server/startup/start.sh
Here is what is in my stop.sh script:
#! /bin/sh
screen -r server -X quit
Everything runs normally if I run it using sh, and I only encounter a problem when using cron.
From what I see there could be 2 possible problems:
If the lines you are running in crontab are (and only those):
home/server/startup/stop.sh
home/server/startup/start.sh
then you are missing the time part of the line. If you want to run your program only once on boot you can run:
#reboot home/server/startup/start.sh
You are not giving the full path to your program (possibly you are just missing a / in the begging). Try running
* * * * * /home/server/startup/start.sh
or
#reboot /home/server/startup/start.sh
If these don't work I recommend you try the following to troubleshoot the issue:
Run the command using sh in the cron:
* * * * * /bin/sh /home/server/startup/start.sh
Try redirecting the stdout and stderr of your command to a file and see if any errors occur
I am running a bash script that transfers files to my AWS bucket.If i run the bash script through my terminal it works fine (via ./myBash.sh).
However I put it in my crontab but there it doesn't work.This is my bash script
#!/bin/bash
s3cmd put /home/anonymous/commLogs.txt s3://myBucket/
echo transfer completed
echo now listing files in the s3 bucket
s3cmd ls s3://myBucket/
echo check
And this is my crontab-
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
46 13 * * * /bin/bash myBash.sh
And here is a list of things i have aready tried -
1)tried running the crontab with a node app to test whether crontab was working(the answer was yes)
2)tried running the crontab without the SHELL and PATH
3)Tried running the bash script from cron using sudo (46 13 * * * sudo myBash.sh)
4)tried running the bash without the /bin/bash
5) Searched many sites on the net for an answer without satisfactory results
Can anyone help me with what the problem may be?(I am running Ubuntu 14.04)
After a long time getting the same error, I just did this :
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
For anyone coming to this post.
I was having same problem and reason was crontab was running under root user and s3cmd was configured under ubuntu user.
so need to copy .s3cfg to root
cp -i /home/ubuntu/.s3cfg /root/.s3cfg
Trying to add a new cronjob to my server using Plesk. I have two running already, but it's so long since I set them up that I can't remember why I did it the way I did. They work exactly as they should.
*/13* * * * &> /dev/null php -q httpdocs/forum/notifyreply.php
*/9 * * * * &> /dev/null php -q httpdocs/forum/notifytopic.php
However, when I add my new job using the same format, it doesn't seem to do anything. The script doesn't run and I get no e-mail notification to tell me that the script has run. The new job is as follows:
* * * * * &> /dev/null php -q httpdocs/crm/autoMessages/autoEmail.php
I'm running these on a Linux Virtual Server with Apache and using Plesk Control Panel. Hosting provider is 123-Reg.
Can anyone help?
Thanks!
UPDATE
So I've now removed the /dev/null line so I receive e-mail notifications and I'm getting the following error message:
/bin/sh: php: Permission denied
What I don't understand is why permission is being denied on this command but not on the other two...
Not sure what could have happened, but first thing I would do is to check if the permissions for the new php script are the same as what the old ones have. Do a ls -l on both httpdocs/forum/notifyreply.php and httpdocs/forum/notifytopic.php, and compare the permissions against what httpdocs/crm/autoMessages/autoEmail.php have.
You are not setting a User/Group for the cron command, better do it like this
* * * * * www-data www-data php -q httpdocs/crm/autoMessages/autoEmail.php > /dev/null 2>&1
(assuming that www-data is the correct User)
note that I put the error handling at the end.
Also, I think that you should use the full path; either you forgot to add a slash ( /httpdocs/.... ) , or do you have this script in the cron folder?
I am trying to figure out how to run a SSH command via cron for linux. The command I want to run is:
svn update /path/to/working/dir
Something like:
*/1 * * * * root ssh svn update /path/to/working/dir
Anyone know what I would need to do with the cron line?
EDIT: I don't need it to be SSH, just need to run svn update on the same server as cron to the working directory.
EDIT 2: What I was looking for was:
*/1 * * * * svn update /path/to/your/working/copy
I worded it incorrectly though, asking too specific about SSH, so I awarded the answer that talks about cron via SSH specifically, but if anyone wants to know how to do it locally, you don't need SSH.
You must compare the environment variables. I've wrote a simple bash script to do that for me:
#!/bin/bash
env
echo $PATH
type -a svn
cd /home/<username>
svn info
exit 0
And save it in /home//crontest.sh
Then you execute the code by hand and write the result into a file:
/home/<username>/crontest.sh > /home/<username>/hand_env
Then in your crontab:
* * * * * /home/<username/crontest.sh > /home/<username>/cron_env
After that you have two files, you will probably see that there are some differences between the environment variables.
The solution to the svn and cron problem is to set the environment variables in bash script of svn update to those obtained by hand it should look something like this (parts 2>/dev/null and exit 0 ARE VERY IMPORTANT):
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X$
export MAIL=/var/mail/root
export _=/usr/bin/env
export PWD=/home/tv
export LANG=en_US.UTF-8
export HOME=/home/tv
export SHLVL=2
export LOGNAME=root
#export LESSOPEN=| /usr/bin/lesspipe %s
#export LESSCLOSE=/usr/bin/lesspipe %s %s
export SHELL=/bin/bash
svn update https://localhost/svn /var/www/<dir> 2>/dev/null
exit 0
Write this script in for eg. /etc/init.d/skrypty/cron.sh
Then you just put in your crontab (i've done it as root)
* * * * * /etc/init.d/skrypty/cron.sh >/dev/null 2>&1
You need to pass the host name (and also the username, unless you want to log in as root) to SSH:
*/1 * * * * root ssh user#hostname svn update /path/to/working/dir
Also see ssh --help for more information.
Note that you'd need to input your password with SSH, unless you've setup your SSH to be able to log in without password
I have faced the same issue where I had edited crontab as root user. I have working copy under my home (/home/myname/project) directory and I did edit crontab as myname user and it worked.
0 22 * * * bash /home/myname/svn.sh
svn.sh has the following lines
#!/bin/sh
svn up /home/myname/project
I'm trying to use a cronjob to run a ruby script (Using Rails3 runner) with the following Cronjobs:
#!/bin/bash
0-59 * * * * echo 'script test'
# Begin Whenever generated tasks for: test1
* * * * * /bin/bash -l -c '/home/administrator/test1/script/rails runner /home/administrator/test1/app/create_flag.rb >> /home/administrator/test1/test.log 2>&1'
# End Whenever generated tasks for: test1
test1 is the name of the Rails3 project folder.
the "echo 'script test'" was added as a test, but neither seems to be executing. I'm currently using Ubuntu 10.04 LTS.
Have I written the cronjob incorrectly?
Crontab file is not a shell script. So you don't need #!/bin/bash at the beginning of the file. Plus, spaces there are suspicious. Try something like this:
SHELL=/bin/bash
MAILTO=administrator#localhost
BASH_ENV=/home/administrator/.bash_profile
* * * * * /home/administrator/test1/script/rails runner /home/administrator/test1/app/create_flag.rb >> /home/administrator/test1/test.log 2>&1'
Plus, make sure you call crontab -e as administrator to edit the crontab file.
You need to specify the user which runs the commands (you can see the format here. Also the echo will output 'script test' to what? If you want a test try doing a touch on a file, so you can physically see the action of the cron job.
Cron does not use your user environment, so it will not have the same path set that you have. This means that you should use absolute paths for commands.