How do I run screen and run a command inside of screen automatically?

CosmicWanderer

New Member
Joined
Feb 17, 2026
Messages
13
Reaction score
12
Credits
116
I have a game server that I want to automatically run whenever I start up the physical server. For a while I just used systemd to start the server directly, but this game server has a console that I want to be able to access at runtime. Ideally, I would like to have some sort of setup where a detached screen is created, the program is run, and I can attach the screen when I want to use it. Is that possible and if so, how do I do it?
 


presumably you would need to start the game server as a service and configure it to start on boot, then pipe the output to the terminal or something. no idea really.

a lot more information is needed. what distro? what DE? what game? probably other specific information, this kind of thing is a bit outside of my personal scope of knowledge
 
I have a console based system monitor tool that I can run in a screen session and then access the running session later from a remote login. The program is named "conmon" so...

at boot time I can run

Code:
screen -S conmon conmon

where the "-S conmon" option specifies the session name by which you will access the process from another screen session. (Lacking the "-S conmon" argument, the session name will be something like "<pid>.pts-0.<hostname>" )

To attach to the session later, I log into the server and run

Code:
screen -rd conmon

where -d disconnects any other attached session and -r reconnects it to this session.

I wrote a little wrapper script that takes a program name and, if a screen session exists with that session name, connects to it. If no such session exists, it starts one. It's kind of simplistic, but it was written for a very simple need.
 
I have a game server that I want to automatically run whenever I start up the physical server. For a while I just used systemd to start the server directly, but this game server has a console that I want to be able to access at runtime. Ideally, I would like to have some sort of setup where a detached screen is created, the program is run, and I can attach the screen when I want to use it. Is that possible and if so, how do I do it?
open crontab edit mode:
Code:
crontab -e
crontab works on everything, systemd or not, it's easy to add, and runs your scripts with user privileges automatically.
Add a new line for a screen session:
Code:
@reboot screen -dmS name_of_your_session /home/user/path/to/your/game
Save and exit. You can list your crontab jobs by typing:
Code:
crontab -l

Parameters used in startup entry:
-d -m Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
-S adds session name for easier management

After reboot, use screenie tool (install it) to easily manage screen sessions running in the background:
Code:
screenie

Once you are in screenie, just type screen number you want to enter, in your case it's 1.
Background screen session allows you to see full output, you can also scroll the terminal by key press combination:
  1. CTRL+ALT+A Enter Copy mode
  2. scroll terminal output by using arrow and PgUp PgDown keys
  3. Esc - exit Copy mode
To detach from screen and go back to screenie menu, press
CTRL + A + D
 
Last edited:
Thanks, I added a cronjob
Code:
@reboot /usr/bin/screen -dmS [screen_name] bash -c 'cd /home/[path_to_parent] && exec /usr/bin/java -Xms4G -Xmx5G -jar game_server.jar nogui'
when I tried to attach it, it gave me an error Directory '/run/screen' must have mode 755. I then used chmod to fix permissions, but after that it said that the screen I was trying to attach did not exist. I rebooted and tried again... same thing happened. I tested the command in terminal (without the @reboot part of course) and it worked well.

In case it matters, I am running rocky linux with no DE.
 
Thanks, I added a cronjob
Code:
@reboot /usr/bin/screen -dmS [screen_name] bash -c 'cd /home/[path_to_parent] && exec /usr/bin/java -Xms4G -Xmx5G -jar game_server.jar nogui'
when I tried to attach it, it gave me an error Directory '/run/screen' must have mode 755. I then used chmod to fix permissions, but after that it said that the screen I was trying to attach did not exist. I rebooted and tried again... same thing happened. I tested the command in terminal (without the @reboot part of course) and it worked well.

In case it matters, I am running rocky linux with no DE.
Perhaps include a PATH variable that includes the paths of all executables in the definition, in particular the
/home path. Check out this post for some details: https://www.linux.org/threads/crontab-audio-alert-just-before-the-hour-debian-13.65784/post-305731.
 


Follow Linux.org

Members online

No members online now.

Top