start tmux session from crontab

922 views
Skip to first unread message

Gabriel Lupini

unread,
Jan 13, 2022, 10:20:44 AM1/13/22
to tmux-users
Hi guys,

We've been trying to make a cronjob to create a new session of tmux after reboot, so later after an ssh connection or directly with a screen located in /dev/tty1 we can attach the shown terminal to the already made session. 

After making a simple .sh to do so in "sudo crontab -e":
@reboot /bin/bash /home/USER/start.sh

With start.sh as
#!/bin/bash
/bin/sleep 5
source /home/debian/.bashrc
/usr/bin/tmux new-session -d -s session1
/usr/bin/tmux send-keys -t session1 "source /home/USER/.bashrc" C-m
/usr/bin/tmux send-keys -t session1 "echo \"hello\"" C-m

After connecting via ssh and doing "tmux attach-session -t session1" in that terminal we get the message "no sessions"

The opened terminal tty is "/dev/pts/1" when I run this script so I assume the usual "/dev/pts/0" is being occupied during the cronjob, but we find ourselves unable to access it. Any idea of what we are doing wrong?

Any help would be appreciated, Thanks!

Nicholas Marriott

unread,
Jan 13, 2022, 11:37:11 AM1/13/22
to Gabriel Lupini, tmux-users

/dev/pts/0 being in use will make no difference.

You put this in root's crontab, so tmux will be running as root, is that what you intend?

If so and you still cannot attach as root, then likely your script is failing for some other reason so you will need to debug it.

Does the script work if you run it manually?

What if you run it from cron like this:

    /bin/bash -x /path/to/script 2>/tmp/log

Does /tmp/log show errors? You may want to add "|| exit 1" to all the commands to work out which one it stops at.




--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/tmux-users/60a26273-02f3-4f9d-a460-a9f3c63b6634n%40googlegroups.com.

Gabriel Lupini

unread,
Jan 14, 2022, 3:44:54 AM1/14/22
to tmux-users
What I finally did as a workaround is, instead of doing this tmux commands in a cronjob, adding them to the ~/.bashrc file. 
So every time a terminal is opened (whether is a screen (tty) or through ssh (pts)) if the tmux session doesn´t exist, it creates it. This way there is always this session available as soon as a terminal is opened. The added commands are:

/usr/bin/tmux has-session -t smartbox 2>/dev/null
$SESSION= sessionname
if [ "$?" -eq 1 ] ; then
    /usr/bin/tmux new-session -s $SESSION -d -x
   #whatever format/commands you want your session to have by default, ex:
    /usr/bin/tmux split-window -t $SESSION  -v
fi
Reply all
Reply to author
Forward
0 new messages