4

I'm having problem with crontab when I'm running a script.

My sudo crontab -e looks like this:

05 00 * * * /opt/mcserver/backup.sh
10 00 * * * /opt/mcserver/suspend.sh
05 08 * * * /sbin/shutdown -r +1
11 11 * * * /opt/mcserver/start.sh  <--- This isn't working

And the start.sh looks like this:

#!/bin/sh
screen java -d64 -Xincgc -Xmx2048M -jar craftbukkit.jar nogui

and have these permissions (ls -l output)

-rwxr-xr-x 1 eve eve  72 Nov 24 14:17 start.sh

I can run the command from the terminal, either using sudo or not

./start.sh

But it wont start with crontab. If i do

grep -iR "start.sh" /var/log

I get the following output

/var/log/syslog:Nov 27 11:11:01 eve-desk CRON[5204]: (root) CMD (eve /opt/mcserver/start.sh)
grep: /var/log/btmp: Permission denied
grep: /var/log/lightdm/x-0-greeter.log: Permission denied
grep: /var/log/lightdm/lightdm.log: Permission denied
grep: /var/log/lightdm/x-0.log: Permission denied

So my question is, why isn't it working? And since my script run without using sudo, I don't necessarily need to put it in sudo crontab?

( and I'm using Ubuntu 12.10 )

Thanks in advance, Philip


Answer to twalberg's response

1. Changed owner on craftbukkit to root, to see if that fixed the problem.

-rw-r--r-- 1 root root 12084211 Nov 21 02:14 craftbukkit.jar

and also added an explicit cd in my start.sh script as such:

#!/bin/sh
cd /opt/mcserver/
screen java -d64 -Xincgc -Xmx2048M -jar craftbukkit.jar nogui

2. I'm not quite sure what you mean here. Should I use the following path in my start.sh file when i start java? (output from which java)

/usr/bin/java

3. When my server closes, screen is terminated. Is it a good idea to start screen in "detached mode" anyway?

Still got the same "Permission denied" error.


Problem solved! By using the proper flag on screen, as below, it is now working as it should!

screen -d -m java -d64 -Xincgc -Xmx2048M -jar craftbukkit.jar nogui

Thanks a lot to those who answered, and especially twalberg!

Philip Larson
  • 51
  • 1
  • 1
  • 5
  • 1
    On #2 - using `/usr/bin/java` should be fine if that's the one you usually use (by just typing `java`) - I didn't know for sure though whether you used a different location, so I didn't suggest a specific path. For #3, I've never tried starting `screen` without a controlling terminal available, so I suggested that possibility - not sure if it's necessary, but worth trying. And the "Permission denied" is coming from `grep` not being able to read those files - not from your `cron` job failing in any way. Is `cron` e-mailing any output from the script? – twalberg Nov 28 '12 at 20:25
  • I (stupidly) didnt set up croncab to send me e-mail. But now when I did, i got this: Must be connected to a terminal. So I guess the problew has to do with screen? – Philip Larson Nov 28 '12 at 21:26
  • And now it works! The problem had to do with not using _screen -d -m_ in my script! – Philip Larson Nov 28 '12 at 21:29

3 Answers3

1

start.sh is owned by "eve:eve" and your crontab is running as root.

You can solve this by running following command

chown root:root /opt/craftbukkit/start.sh 

Your crontab will be running as root though.

Tip: When running bash in crontab always use absolute paths (it will make debugging a lot easier).

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Niclas Larsson
  • 1,317
  • 8
  • 13
  • 1
    Thanks for answering, but this didn't help. /var/log/syslog:Nov 27 11:41:01 eve-desk CRON[5445]: (root) CMD (/opt/mcserver/start.sh) grep: /var/log/btmp: Permission denied grep: /var/log/lightdm/x-0-greeter.log: Permission denied grep: /var/log/lightdm/lightdm.log: Permission denied grep: /var/log/lightdm/x-0.log: Permission denied Changed the owner to: -rwxr-xr-x 1 root root 72 Nov 24 14:17 start.sh – Philip Larson Nov 27 '12 at 10:43
  • Not really relevant, as `root` has permission to read and execute `start.sh` (although we don't know what the permissions are on, e.g. `craftbukkit.jar` and any other files that `root` will eventually need to touch as a result of running `start.sh`. – twalberg Nov 28 '12 at 17:46
1

Here are some things to check:

  1. root obviously has read/execute permissions on start.sh, but what are the permissions on craftbukkit.jar - can root read it? You may also want to add an explicit cd /path/to/where/craftbukkit.jar/is in your start.sh script.
  2. Is java in root's default path within cron? Note that this path is not necessarily the same as the one that you get via sudo, su or directly logging in as root - it's typically much more restricted. Use full path names to both java and craftbukkit.jar to work around that.
  3. Since screen will not start with a terminal available, you may need screen -d -m ... instead. Hopefully, you intend to eventually attach to each screen instance and terminate it later, or you have arranged for it to terminate automatically when the script is done...
  4. The /var/log/syslog entry shows that cron did in fact execute the script, so it must have failed for one of the above reasons (or something else I haven't noticed yet)
  5. The other errors from grep are simply due to the fact your non-root user does not have permission to read those specific files (this is normal, and a good thing).
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
twalberg
  • 59,951
  • 11
  • 89
  • 84
0

The log shows the user has no access to dir " /var/log/", You should set the log files' permition for the cron's owner.

shiwenlu518
  • 446
  • 3
  • 7