Run the same command by hand first and confirm login and the model. Then put it in a script: pin the working directory, set a timeout, and redirect output to a file.
-p is for a patch note, a review summary, or a read-only question. Multi-turn file edits need an interactive session or a non-interactive mode with a clear stop. Do not hide conversation state in a pipe.
Do not auto-retry writes in a script. Fail with a non-zero exit. Put tokens in the environment, not in argv that shows up in process lists.
See the Grok Build install and troubleshooting chapters. This page is the script entry, not a copy of every CLI flag. Flags come from today official help.