twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.85k stars 477 forks source link

"exec format error" when running a script-template. #2584

Closed sohmc closed 1 year ago

sohmc commented 1 year ago

Describe the bug

I am attempting to create a script that will install a cronjob for chezmoi. The script is templated so that if it detects linux, then the bash script will output.

When I do chezmoi -v apply, I get the following error:

To reproduce

  1. Run my dotfiles

Expected behavior

I expect the script to run without issue.

Output of command with the --verbose flag

diff --git a/0000-set-up-chezmoi-cronjob b/0000-set-up-chezmoi-cronjob
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..21491bb9a2b75d87d3fb77328c63f90736a7da61 100755
--- a/0000-set-up-chezmoi-cronjob
+++ b/0000-set-up-chezmoi-cronjob
@@ -0,0 +1,12 @@
+
+#!/bin/bash -xe
+
+# crontab/chezmoi hash: cdf388cd548dcfd638cc04d53dc4f275f84c387a3d87b543f4683df3814073c0
+NEW_CRONTAB=$(cat linux/crontab/chezmoi)
+
+echo "Deleting existing chezmoi cronjob"
+crontab -l | grep -v 'bin/chezmoi' | crontab -
+
+echo "Adding chezmoi cronjob"
+crontab -l | echo "${NEW_CRONTAB}" | crontab -
+
chezmoi: fork/exec /tmp/3782778879.0000-set-up-chezmoi-cronjob: exec format error

Output of chezmoi doctor

```console RESULT CHECK MESSAGE ok version v2.25.0, commit b3a8879e30a20134c9bef48646055e57aa78d8c5, built at 2022-10-13T14:01:32Z, built by goreleaser warning latest-version v2.27.1 ok os-arch linux/amd64 (Ubuntu 22.04.1 LTS (Jammy Jellyfish)) ok uname Linux starbuck 5.15.0-53-generic #59-Ubuntu SMP Mon Oct 17 18:53:30 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux ok go-version go1.19.2 (gc) ok executable ~/.local/bin/chezmoi ok upgrade-method replace-executable ok config-file no config file found ok source-dir ~/.local/share/chezmoi is a git working tree (clean) ok suspicious-entries no suspicious entries ok working-tree ~/.local/share/chezmoi is a git working tree (clean) ok dest-dir ~ is a directory ok umask 002 ok cd-command found /bin/bash ok cd-args /bin/bash info diff-command not set ok edit-command found /usr/bin/vi ok edit-args /usr/bin/vi ok git-command found /usr/bin/git, version 2.34.1 ok merge-command found /usr/bin/vimdiff ok shell-command found /bin/bash ok shell-args /bin/bash info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.27 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

My end state is to have chezmoi install a crontab on machines where it is installed to apply chezmoi automatically once an hour. If there is a better way for this, I'm happy to do that instead.

There is a previous issue #666 that has the same error. But in my case, I don't have an existing runfile, besides the one I'm templating.

bradenhilton commented 1 year ago

Is it possible that this is caused by the newline that precedes the shebang?

What are the contents of the template?

Also, unsure of the specific reason, but it appears you aren't running the latest version of chezmoi.

twpayne commented 1 year ago

Is it possible that this is caused by the newline that precedes the shebang?

This is definitely the reason.

sohmc commented 1 year ago

Yup, that was it. I added a - to the end curly brace in if statement and it "works"

Technically getting a different error but this one seems bash related. Thanks for the quick assist.

twpayne commented 1 year ago

Looking at your crontab entry, you're using ~ to represent your home directory. The expansion of ~ is typically done by the shell and is not available in other places (e.g. it's not normally available in crontabs). It's safer to use the environment variable $HOME instead.

sohmc commented 1 year ago

@twpayne coming in with an unrelated assist. Thank you! didn't know that. It's always worked for me.

This commit is for you!