bashgoogle-cloud-platformvirtual-machinetmuxstartupscript

How to run a tmux session as startup_script in google cloud?


I want to run a python script in a tmux session on startup when launching my google cloud vm. I have searched around stack overflow and found this piece of code.

#! /bin/bash
sudo -H -u MyUser tmux new-session -d -s discord 'python3 MyFile.py'

I placed this in the meta data part of my vm where startup_scripts go but it doesn't launch when i start my vm. However when I run this code in the terminal after my vm has started it does exactly what I want it to do. What am I missing here?


Solution

  • After digging around for I while I found the problem. The command runs in the root directory so, before your piece of code, you have to add:

    Add: cd home/username
    

    Before your code.