I have a LaTeX assignment, but I don’t want to install tex live on my own laptop because it contains thousands of small files. Maybe it can slightly protect my hard driver. Then I have to find a way to compile the LaTeX files.
Firstly, I choose to use Overleaf - an online LaTeX tool set. It works quite good at uni, but when I get home it generally overrides my texts with an old version because of my slow network speed. That means it’s unstable in poor network environment.
So, I began to think about directly using my uni’s shared server which is installed with tex live in the last year.
No-password access
Using ssh or scp requires password every time, that makes one-click solution not work. So, the first step is to remove the password verification. So, the method is like git push key.
cd ~/.ssh
cat id_rsa.pub
Note, if you don’t have this file or folder, please check this post.
After this, I can see the public key. Then, copy/append them into uni’s shared server, in this file: ~/.ssh/authorized_keys. (Note: the key only has one line)
Now, I can access that server without entering password any more.
Automatic process
The next thing is to make everything work automatically.
Process
The process is like this:
Me: push to GitHub
Uni: pull from GitHub
Uni: compile
Me: Pull from uni
Access to private git repo
The idea is, GitHub provides each project a deploy key settings, which allow user to set ssh keys with pull permission only. Uni’s shared server is not trusted, so I don’t want to give all my account’s access to that server.
Execute scripts in my PC
I want to pass command into ssh command directly. So I searched for that,
ssh user@remote 'bash -s' < local_script.sh
With this command, I can run local commands on remote server directly.
Scripts
Here are the file contents.
remote_old.sh
cd ~/Codes/ec-groupwork/reports/report1
git pull
pdflatex main.tex
pull_old.sh
ssh user@remote 'sh -s' < remote_old.sh
scp user@remote:~/Codes/ec-groupwork/reports/report1/main.pdf main.pdf
A newer version just skip the GitHub server.
remote.sh
cd ~/Codes/ec-groupwork/reports/report1
pdflatex main.tex
pull.sh
scp main.tex user@remote:~/Codes/ec-groupwork/reports/report1/
ssh user@remote 'sh -s' < remote.sh
scp user@remote:~/Codes/ec-groupwork/reports/report1/main.pdf main.pdf
Note: I didn’t use bash because uni’s server is running CShell.
So far, I can run ./pull.sh to compile & pull back the .pdf file easily~~~ Rejoice for saving my hard driver ;) !
我有个 LaTeX 作业要写,但实在不想在自己笔记本上装 tex live,因为它包含成千上万个小文件——不装它说不定还能稍微保护一下我的硬盘。于是我得另寻他法来编译 LaTeX 文件。
一开始,我选了 Overleaf——一个在线 LaTeX 工具平台。在学校用的时候体验还不错,但一回到家,由于网速太慢,它经常拿旧版本把我的新内容给覆盖掉。也就是说,它在弱网环境下很不稳定。
于是,我开始琢磨直接利用学校的共享服务器,去年那上面就已经装好 tex live 了。
免密登录
每次用 ssh 或 scp 都要输密码,这样就没法实现一键操作了。所以第一步就是去掉密码验证,做法就跟配置 git push key 一样。
cd ~/.ssh
cat id_rsa.pub
注意:如果你没有这个文件或目录,请参考这篇文章。
执行完就能看到公钥了。然后把它复制/追加到学校共享服务器的 ~/.ssh/authorized_keys 文件里。(注意:公钥内容只有一行)
现在,我再访问那台服务器就不用每次都输密码啦。
自动化流程
接下来就是让整个过程自动跑起来。
流程
大致流程如下:
我:push 到 GitHub
学校服务器:从 GitHub pull
学校服务器:编译
我:从学校服务器拉取成品
访问私有 Git 仓库
思路是这样的:GitHub 为每个项目提供了 Deploy Key 设置,允许用户配置只有 pull 权限的 SSH 密钥。学校的共享服务器毕竟不是完全可信的,我可不想把我整个账号的权限都交给它。
在本地电脑上远程执行脚本
我想直接把命令传给 ssh 执行。查了一下找到这个方法:
ssh user@remote 'bash -s' < local_script.sh
用这条命令,我就能直接把本地脚本里的命令放到远程服务器上跑。
脚本内容
下面是具体的文件内容。
remote_old.sh
cd ~/Codes/ec-groupwork/reports/report1
git pull
pdflatex main.tex
pull_old.sh
ssh user@remote 'sh -s' < remote_old.sh
scp user@remote:~/Codes/ec-groupwork/reports/report1/main.pdf main.pdf
新版脚本直接跳过了 GitHub 服务器。
remote.sh
cd ~/Codes/ec-groupwork/reports/report1
pdflatex main.tex
pull.sh
scp main.tex user@remote:~/Codes/ec-groupwork/reports/report1/
ssh user@remote 'sh -s' < remote.sh
scp user@remote:~/Codes/ec-groupwork/reports/report1/main.pdf main.pdf
注意:我没用 bash,因为学校服务器跑的是 CShell。
至此,我只要运行 ./pull.sh 就能轻松完成编译并把 .pdf 文件拉回本地啦~~~ 为成功省下硬盘空间欢呼 ;) !