headless/unattended operation

razorboy

Member
I swear I discussed this with someone, but after lots of searches on this and the official KoL forums, the most I can find is that you can run mafia in a command window using the "--CLI" command.

1. I'd like to run mafia as a cron job (except on windows), and I'm up to the part where I need to login. I know I can set it to autologin, but I'd rather pass it the username/password via the command line at launch. Can I do that? I remember asking this at one point and the response was something about pointing to a different user directory or something for each account you want to use. Is this still the case?

2. Also, how do I automatically run a script at startup?


Sorry if this has been covered ad nauseum, but I swear I couldn't find the details after a prolonged search!
 

lostcalpolydude

Developer
Staff member
You can tell it to use a script where the first line is "login username". You could also have "logout" followed by "login username2 if needed. I don't remember the syntax for telling it to use a script. This assumes you let mafia save your password(s).
 

Grotfang

Developer
1. I'd like to run mafia as a cron job (except on windows), and I'm up to the part where I need to login. I know I can set it to autologin, but I'd rather pass it the username/password via the command line at launch. Can I do that? I remember asking this at one point and the response was something about pointing to a different user directory or something for each account you want to use. Is this still the case?

2. Also, how do I automatically run a script at startup?

Okay. So, this thread title is slightly off. Headless is a specific property of java that mafia does support. For those future peeps (hi future peeps!) who come looking, the code you might be looking for (that I run) is this:

Code:
/usr/bin/nohup java -DuseCWDasROOT -jar -Djava.awt.headless=true /home/Grotfang/<botname>/KoLmafia.jar login.txt --CLI > /home/Grotfang/<botname>/output &

This is on a linux box. I assume that the headless command is similar, but I don't really understand why you would use it on a windows machine (unless it's a windows server?). Note that if you're going headless, you might want to pipe the output from the program into a file (called "output" here). Oh, and for those on linux, that ampersand at the end is important.

On to the topic of your post, razorboy.

You can see from the example above that to call a CLI script is really easy. Save a batch file (file.bat) in your windows directory and make it contain the following:

Code:
java -jar KoLmafia.jar login.txt --CLI

Create a file called login.txt in that directory (the one with KoLmafia.jar AND the .bat file) that has the following in:

Code:
login character
call script.ash
logout

You need to make sure that mafia remembers your character's password. If you want to run a second character, it's very simple. Either extend the file:

Code:
login character1
call script1.ash
logout
login character2
call script2.ash
logout

OR have a second directory with a copy of mafia and a second batch file that you separately schedule via Windows Scheduler.
 
Last edited:

xKiv

Active member
And please don't put passwords on command lines. They are visible to anybody who is logged in on the machine, which might be less difficult than you think.
 
Last edited:

razorboy

Member
Thanks, guys!

Can we possibly put this info on the wiki? I tried looking there, as well, but nothing pertinent came up on searches for "headless" or "unattended".

EDIT: I just gave it a try and it works like a champ! Thanks again!
 
Last edited:

Rinn

Developer
You can also pass in an ash script in the same if that's preferable

Code:
java -jar KoLMafia-latest.jar --CLI automation.ash

Code:
void main()
{
    cli_execute("login character");
    // Do Whatever
    cli_execute("quit");
}

I put the commands in a batch file and call that file from the scheduled task, for ease of editing (I also have it update my mafia version, send a growl message when it finishes, etc).

Also it's a wiki you can add that information yourself if you'd like :)
 
Last edited:

StDoodle

Minion
You could even use the same script for manual and automated adventuring if you wanted by adding a check for my_name() == "" before the login (and probably save the results of that for figuring auto-logout too).
 

Grotfang

Developer
If you do decide to add it to the wiki, please don't use the word "headless". Headless has a specific meaning, which is not what you are talking about here (and probably why searching didn't come up with anything useful). Running a headless environment means you do not have a monitor/keyboard/mouse. It is only a small subset of our users who want to do stuff like that. What you are doing is scheduling a mafia instance on windows.

For what it's worth, searching "schedule windows" produces this thread, where Rinn posts what he has put above. I would recommend you read that thread further, as fianor includes a useful demonstration of how to handle multiple characters. Some of this may be worth putting on the wiki, if you think sufficient users will want to be able to readily find this information.
 
Last edited:

razorboy

Member
You could even use the same script for manual and automated adventuring if you wanted by adding a check for my_name() == "" before the login (and probably save the results of that for figuring auto-logout too).

This confuses me. How would a name check ever work if you aren't logged in?
 

Grotfang

Developer
I could be wrong, but I think what's being referred to is that the script can tell whether it needs to log you in or whether you have called it once you have logged in. Eg:

Code:
boolean logIn = false;
void main() {
  if( my_name() == "" ) {
    cli_execute( "login character" );
    if( my_name != "" ) {
      logIn = true;
    } else {
      abort();
    }
  }
  // DO AWESOME STUFF
  if( logIn ) {
    cli_execute( "logout" );
  }
}
 
Last edited:

StDoodle

Minion
Yup, exactly; a few of the my_SOMETHING() functions return a blank string when no characters are logged in, which is never a valid result for a logged-in character, so you can test logged-in-edness (totally a word) that way.
 

Catch-22

Active member
Just to add to what's here. If you use the login command without logging out, it will log out your current session for you (saves a line in your script). Also, if you're automating, at the end of your login.txt you should put exit (which closes KoLmafia and also logs out).

eg.

login character1
call bounty.ash *
login character2
call superdrinks.ash
exit
 
Top