How do I use crontab to start a screen session?

46,584

Solution 1

Something like this should work. This example spawns a screen and runs "top":

screen -d -m top

In your crontab, as indicated, you'd want to do something like this:

@reboot /usr/bin/screen -dmS gameserver-screen /opt/mycoolgame/bin/gameserver

Of course, if the game server requires a "normal" environment set, you can get closer by:

@reboot (. ~/.profile; /usr/bin/screen -dmS gameserver-screen /opt/mycoolgame/bin/gameserver)

Solution 2

This should be sufficient...run

$ crontab -e

Then enter:

@reboot screen -dmS Victor

Solution 3

Just for completeness' sake, it's also possible to use tmux for the purpose instead of screen (see this link for a comparison):

@reboot tmux new-session -d -s yourNameOfTheSession "your command to run"

Solution 4

It needs to be noted that simply using screen -dmS SessionNameHere someCommand is NOT enough. In many envs (IE: Ubuntu 18.x) you also have to give the tmp dir or you cant re-attach to the running screen, you wont even be able to list them. The CORRECT way to run screen in crontab is like this:

 * * * * * export SCREENDIR=~/tmp ; screen -dmS SessionNameHEre SomeCommand

So if you cant connect to a session started by cron (I had this issue with Ubuntu 18), find out what screen is using for a tmp dir, and put that in the crontab entry. You can do that with "echo $SCREENDIR"

Share:
46,584

Related videos on Youtube

Victor
Author by

Victor

[insert description here]

Updated on September 17, 2022

Comments

  • Victor
    Victor almost 2 years

    I want to create a crontab entry so that it starts screen, starts a gameserver and detaches. This is for in case the server is rebooted and I want it to automatically start this for me.

    0 0 0 0 0 (command)

    should run upon startup.

    It runs a shell file located at ~/cube/server.sh

    • Victor Matheus Alves Ramos
      Victor Matheus Alves Ramos over 13 years
      Are you a user on this machine, or do you have access to init scripts?
    • Dennis Williamson
      Dennis Williamson over 13 years
      Please see Process Management.
    • Victor Matheus Alves Ramos
      Victor Matheus Alves Ramos over 13 years
      @Dennis: Yep, I like that. But we can't assume the game server he's running doesn't have some sort of console that he'd need to access (i.e. always runs in the foreground). Else why would he bother with screen? Unless of course, he's not aware of nohup and backgrounding.
    • Victor
      Victor over 13 years
      I am a user on this machine.
  • Victor
    Victor over 13 years
    what does the . ~/.profile part do?
  • Victor Matheus Alves Ramos
    Victor Matheus Alves Ramos over 13 years
    It forcibly sets the environment for the cron entry. Without it, you just get a couple very specific entries. (See man crontab for details)
  • Victor
    Victor over 13 years
    I am have a .sh file that executes the game server, so would this work? @reboot (. ~/cube; /usr/bin/screen -dmS gameserver-screen ./server.sh)
  • Victor Matheus Alves Ramos
    Victor Matheus Alves Ramos over 13 years
    That's probably the best way to do it. If that works, you probably not need to worry about the profile, but if you need a full $PATH, etc, you could always source in the .profile from the server.sh script.
  • Patrick
    Patrick over 8 years
    Can you please try and explain how this 'Victor' command relates to running a script - e.g. the script the OP asked about?
  • atx
    atx over 8 years
    -S is just for the session name
  • Admin
    Admin about 2 years
    I've tried to echo the $SCREENDIR but it's empty. I'm using ubuntu 20.04. My use case is exactly what you have written. I'd like to start a session where I can run a .sh script. I've tried different solutions but it doesn't work. Any tips on how I can run the .sh script in a new session?