I have been debugging this problem for more than a week now and am unable to find a solution. Hoping that one of you points me into correct direction.
I have a workstation pc tucked away that is turned on demand via WOL from another device. The crontab runs perfectly fine when I login via ssh or directly into it prior to the crontab running. If I however do not login beforehand the crontab refuses to run.
It errors out with:
/bin/bash: line 0: cd: /home/user/dir: no such file or directory
and this is the crontab I want to run:
30 7 * * * 6 (cd /home/user/dir && npm run script) >> /var/log/logfile.log 2>&1
I also have some variables set on the top of the crontab file
It is being run by a non-root user, and the files are owned by that user
Thanks for reading,
been stalking the forums for a while, finally have a reason to join
Since you’re running a script in a user’s home directory…
Is the home directory encrypted? By default it typically is on most distributions. So if you’re not logged in it remains encrypted and cron (or systemd rather) has no access to that directory. As it says:
The directory is only mounted once the user unlocks it via login.
As a general rule of thumb you don’t want to be running a script via cron from a user’s directory.
Also from what I can tell…
You didn’t specify whether this is in the system-wide crontab or in the users, but assuming it is in the system-wide (
because otherwise it would not be available either due to encrypted home actually nevermind, user-crontabs aren’t inside the home)… This does not run as a non-root user. You didn’t actually specify a user that is supposed to run this command (or is the
6 the user-ID? Would be weird because they are typically >1000). You would have to specify the user to run the command as:
#<timing> <user> <command>
30 7 * * * 6 user /path/to/command
Thank you so much for the reply,
I haven’t specified the user in the crontab, but I have edited the crontab as the
<user> via crontab command
crontab -e logged in as the
<user> and not the root command
sudo crontab -e so I assumed I don’t need to specify the user in the crontab, should I?
I have tried copying the folder to the
chowned that folder recursively to be owned by the , pointed the crontab to change directory to that folder instead.
I tried rebooting the workstation, looks like i do have home directory encrypted… the change of directory worked without me having to login. Will just need to install npm and node for all users.
Thanks I think I will be able to solve it now
Thank you so much!
No, in that case the user-specific crontab is automatically targeted, so that’s fine.
Glad it worked out
I have managed to get the npm and node scripts working however the script also acesses git and in turn uses ssh key from
I have tried adjusting the HOME path to
/home/<customfolder> and copied gitconfig and .ssh folder there, however the script is still trying to access the
How can I configure .ssh folder to be pointing somewhere else than user’s home folder. Is this even the right approach?
Thanks, I owe you a drink
Mh well generally I don’t think you’d want to move SSH keys outside the user’s home. Because the user home also acts as an access control so the keys can’t be misused (and it’s working apparently ;)).
What you would probably want to do is create a separate SSH key specifically for that task and grant access for that SSH key to your repo. If that key gets compromised you could pull permissions for it and the user can still continue working.
Remember that the whole point of SSH is that each user has a unique key. Yes technically you are executing the script as the same user, but you really don’t want to move the SSH key outside home as stated above.
As for the key location, a quick look around gave me this:
This specifies the file just for git (per repo) and doesn’t require access to the home directory.
Note there is also another answer above thaa but from what I can tell it is written to a config file inside the home directory, which doesn’t help you.
Thank you for the link,
I was able to solve it now, finally!
I tried to use the git config method by adding a line to the repo’s git folder config file:
sshCommand = ssh -i /home/<customfolder>/.ssh/<key> -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
or setting it via commandline:
git config core.sshCommand "ssh -i /home/<customfolder>/.ssh/<key> -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
But this refused to work, it was still looking for the ssh key in the
/home/<user>/.ssh. It started working only after i configured environment variable to be set before the script via
export GIT_SSH_COMMAND="ssh -i /home/<customfolder>/.ssh/<key> -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
that it has started to work. I am not fully happy with the solution but will do for now
I have already created this key just for this cronjob’s purposes with a very limited scope of permissions.
Anyhow, how can I buy you a beer?
All the best.
Mh, odd. Though in that Superuser thread it was also mentioned " that
-i can sometimes be overridden by your config file", so this might be the case. They also go on to say to specify an emtpy config file via
-F /dev/null (an actual empty file should work too). Maybe try that.
Either way, glad it worked out in the end. I don’t drink beer tho