PDA

View Full Version : Cronjob question



Buckwheet
12-02-2015, 11:02 AM
So I have the following script in linux to help with launching characters:



#!/bin/bash
sudo ruby2.0 /root/Desktop/lich/lich.rbw --login Myslut --without-frontend --detachable-client=8000


When calling this from the command line its no problem. The characer is logged in without any issue.

When you put in a simple cronjob like this:



* * * * * /root/Desktop/launch/login.sh >/root/Desktop/log.log 2>&1



You get the following error calling GTK:



/var/lib/gems/2.0.0/gems/gtk2-3.0.7/lib/gtk2.rb:13:in `init': Cannot open display: (Gtk::InitError)
from /var/lib/gems/2.0.0/gems/gtk2-3.0.7/lib/gtk2.rb:13:in `<top (required)>'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:118:in `require'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:118:in `rescue in require'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:124:in `require'
from /root/Desktop/lich/lich.rbw:527:in `<main>'


I am unsure why it can't use GTK. When I use the script I can open up narost and other GTK things.

Suggestions on how else to setup the cronjob?

wildefire
12-02-2015, 12:29 PM
It can't find the DISPLAY environment variable to tell GTK what X server to talk to because it isn't set when called out of cron, but it's in your shell environment when you run it from a terminal. Add an 'export DISPLAY=localhost:0.0' line to your script and try having cron call it, should probably clear it.

Buckwheet
12-02-2015, 01:05 PM
It can't find the DISPLAY environment variable to tell GTK what X server to talk to because it isn't set when called out of cron, but it's in your shell environment when you run it from a terminal. Add an 'export DISPLAY=localhost:0.0' line to your script and try having cron call it, should probably clear it.

I am most likely doing something wrong.

Here is the new crontab:



* * * * * export DISPLAY=localhost:0.0; /root/Desktop/launch/login.sh >/root/Desktop/log.log 2>&1


Still getting the same issue.



/var/lib/gems/2.0.0/gems/gtk2-3.0.7/lib/gtk2.rb:13:in `init': Cannot open display: localhost:0.0 (Gtk::InitError)
from /var/lib/gems/2.0.0/gems/gtk2-3.0.7/lib/gtk2.rb:13:in `<top (required)>'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:118:in `require'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:118:in `rescue in require'
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:124:in `require'
from /root/Desktop/lich/lich.rbw:527:in `<main>'

Lorstrum
12-02-2015, 03:18 PM
export exports for the current user, in your script you are running sudo which changes the user

you can pass it in to sudo like:

sudo DISPLAY=localhost:0.0 ruby2.0 /root/Desktop/lich/lich.rbw --login Myslut --without-frontend --detachable-client=8000

edit: more correct is that export makes available the variable to child processes, sudo starts a new session and cleans its environment. The same would happen if the user was the same before and after the sudo..

Buckwheet
12-02-2015, 04:06 PM
export exports for the current user, in your script you are running sudo which changes the user

you can pass it in to sudo like:

sudo DISPLAY=localhost:0.0 ruby2.0 /root/Desktop/lich/lich.rbw --login Myslut --without-frontend --detachable-client=8000

edit: more correct is that export makes available the variable to child processes, sudo starts a new session and cleans its environment. The same would happen if the user was the same before and after the sudo..

Thanks for the info. However I must not have something setup correctly.

I changed the crontab back to be just:


* * * * * /root/Desktop/launch/login.sh >/root/Desktop/log.log 2>&1


I then changed the script to:



sudo DISPLAY=localhost:0.0 ruby2.0 /root/Desktop/lich/lich.rbw --login Myslut --without-frontend --detachable-client=8000


The error still remains.

BigWorm
12-02-2015, 09:31 PM
Does this machine actually have an X server running?

Either way, that syntax is not valid for the sudo command:



> sudo THING="asdf" echo $THING

>


you probably want something like:



> sudo bash -c 'export THING="asdf"; echo $THING'
asdf
>


but I don't really understand the benefit of using sudo to run as root instead of just adding it to root's crontab instead of your own. But I guess I also don't really understand why you want to run as root anyway.

Buckwheet
12-02-2015, 11:49 PM
Simply because I was asked to try and get something working in linux to log a character in on a schedule. So I was just in the process of writing up two different "guides" if you will on where and how for less technical people.

So I simply found a place that is cheap that can provide Ubuntu 14.04 and turned it into a desktop install and then got lich working. The only part left would be to schedule the logins. I am just not fluent enough with crontab to set it up. I figured I log in via RDP as root so why not use that user to just launch everything.

I can move forward with writing up my guides and if people more experienced than me with linux/crontab want to chime in that would be fine.

Tillmen
12-03-2015, 07:10 AM
root is only needed if you want to change the hosts file, which you're not doing because you're using the --login option.

Here's what I use.

crontab (edited with "crontab -e" while logged in as the appropriate user)

0 8 * * * /home/user/bin/playershops-character 2> /home/user/log/playershops-character.log

playershops-character:


export DISPLAY=:0.0
/usr/bin/ruby /home/user/lich/lich.rb --login Character --without-frontend --detachable-client=8003 --start-scripts=passive-spellup,autoplayershops

Buckwheet
12-03-2015, 07:27 AM
Just out of random curiosity, why id your lich file .rb while ours is .rbw?

Tillmen
12-03-2015, 07:33 AM
Cause I renamed it. On Windows, the .rb extension is run by ruby.exe and .rbw is run by rubyw.exe. The difference being that ruby.exe opens a terminal window and rubyw.exe does not. On Linux, .rbw has no meaning.

Buckwheet
12-03-2015, 07:45 AM
Cause I renamed it. On Windows, the .rb extension is run by ruby.exe and .rbw is run by rubyw.exe. The difference being that ruby.exe opens a terminal window and rubyw.exe does not. On Linux, .rbw has no meaning.

Thanks.

Also, I figured out the crontab issue, the screen was 10.0 not 0.0. Totally forgot about xdpyinfo.